Get the number between specified strings - ruby-on-rails

Ok. Given the example of:
http://example.com/news/3226-some-post-title.html
I'm trying to get the 3226. This regexp: http://interaktywnie.com/newsy/(.*).html
doesn't seem to work. Please help.
Thanks.

You can just use:
/\/(\d+)-(.*)\.html$/
This will grab the digits (\d) after the '/' and put the digits into the first variable once it finds them.
A great place to test regular expression is http://rubular.com/.

You want this:
/http:\/\/example.com\/news\/(\d+)-.+\.html/
\d is any digit. Also, the following site is very useful for regular expressions in ruby:
http://www.rubular.com

"http://example.com/news/3226-some-post-title.html".split("/").last.to_i

Try this pattern:
/http:\/\/example\.com\/news\/(\d+)-.+\.html/
So:
match = /http:\/\/example\.com\/news\/(\d+)-.+\.html/.match("http://example.com/news/3226-some-post-title.html")
puts match[1]

Related

How to remove from string before __

I am building a Rails 5.2 app.
In this app I got outputs from different suppliers (I am building a webshop).
The name of the shipping provider is in this format:
dhl_freight__233433
It could also be in this format:
postal__US-320202
How can I remove all that is before (and including) the __ so all that remains are the things after the ___ like for example 233433.
Perhaps some sort of RegEx.
A very simple approach would be to use String#split and then pick the second part that is the last part in this example:
"dhl_freight__233433".split('__').last
#=> "233433"
"postal__US-320202".split('__').last
#=> "US-320202"
You can use a very simple Regexp and a ask the resulting MatchData for the post_match part:
p "dhl_freight__233433".match(/__/).post_match
# another (magic) way to acces the post_match part:
p $'
Postscript: Learnt something from this question myself: you don't even have to use a RegExp for this to work. Just "asddfg__qwer".match("__").post_match does the trick (it does the conversion to regexp for you)
r = /[^_]+\z/
"dhl_freight__233433"[r] #=> "233433"
"postal__US-320202"[r] #=> "US-320202"
The regular expression matches one or more characters other than an underscore, followed by the end of the string (\z). The ^ at the beginning of the character class reads, "other than any of the characters that follow".
See String#[].
This assumes that the last underscore is preceded by an underscore. If the last underscore is not preceded by an underscore, in which case there should be no match, add a positive lookbehind:
r = /(?<=__[^_]+\z/
This requires the match to be preceded by two underscores.
There are many ruby ways to extract numbers from string. I hope you're trying to fetch numbers out of a string. Here are some of the ways to do so.
Ref- http://www.ruby-forum.com/topic/125709
line.delete("^0-9")
line.scan(/\d/).join('')
line.tr("^0-9", '')
In the above delete is the fastest to trim numbers out of strings.
All of above extracts numbers from string and joins them. If a string is like this "String-with-67829___numbers-09764" outut would be like this "6782909764"
In case if you want the numbers split like this ["67829", "09764"]
line.split(/[^\d]/).reject { |c| c.empty? }
Hope these answers help you! Happy coding :-)

Rails strip all except numbers commas and decimal points

Hi I've been struggling with this for the last hour and am no closer. How exactly do I strip everything except numbers, commas and decimal points from a rails string? The closest I have so far is:-
rate = rate.gsub!(/[^0-9]/i, '')
This strips everything but the numbers. When I try add commas to the expression, everything is getting stripped. I got the aboves from somewhere else and as far as I can gather:
^ = not
Everything to the left of the comma gets replaced by what's in the '' on the right
No idea what the /i does
I'm very new to gsub. Does anyone know of a good tutorial on building expressions?
Thanks
Try:
rate = rate.gsub(/[^0-9,\.]/, '')
Basically, you know the ^ means not when inside the character class brackets [] which you are using, and then you can just add the comma to the list. The decimal needs to be escaped with a backslash because in regular expressions they are a special character that means "match anything".
Also, be aware of whether you are using gsub or gsub!
gsub! has the bang, so it edits the instance of the string you're passing in, rather than returning another one.
So if using gsub! it would be:
rate.gsub!(/[^0-9,\.]/, '')
And rate would be altered.
If you do not want to alter the original variable, then you can use the version without the bang (and assign it to a different var):
cleaned_rate = rate.gsub!(/[^0-9,\.]/, '')
I'd just google for tutorials. I haven't used one. Regexes are a LOT of time and trial and error (and table-flipping).
This is a cool tool to use with a mini cheat-sheet on it for ruby that allows you to quickly edit and test your expression:
http://rubular.com/
You can just add the comma and period in the square-bracketed expression:
rate.gsub(/[^0-9,.]/, '')
You don't need the i for case-insensitivity for numbers and symbols.
There's lots of info on regular expressions, regex, etc. Maybe search for those instead of gsub.
You can use this:
rate = rate.gsub!(/[^0-9\.\,]/g,'')
Also check this out to learn more about regular expressions:
http://www.regexr.com/

Regular expression which allows alphabets, dashes(-) and dot(.)

I'm trying to write a regex which allows alphabets, dot . and dashes - (for validation)
But couldn't find a valid regex which would do so, please help!
Thanks in advance
I think this will work for you
^[a-zA-Z-.]*$
any lowercase letter of the alphabet, any uppercase letter of the alphabet, dash as a group in any combination appearing 1 or many times
This character class should work for you:
[a-zA-Z.-]
Must Read: http://regular-expressions.info
Use this regex ([A-Za-z.-]) and test here http://www.rubular.com/r/H3Axvol13b
(?i)[a-z.-]
(?i) Will find any character no matter what case

Bug in my regular expression

I'm trying to look at a string and reject anything that has seq= or app= in the string. Where it gets tricky is I need elements with q=something or p=something.
The seq= part of the string is always preceded an & and app= is always preceded by a ?
I have absolutely no idea where to start. I've been using http://www.rubular.com/ to try and figure it out but to no avail.
Any help would be hugely appreciated.
Based on your question, I believe you could just reject any strings that match the following expression:
[\?&](?:seq|app)=
This will match any string that contains a ? or & followed by either app= or seq=. The ?: inside the parentheses just tells the regular expression not to bother to capture matching groups as sub-matches. They're not really necessary, but what the heck.
Here's a Rubular link with some samples.

ruby regex - how to match everything up till the character -

given a string as follow:
randomstring1-randomstring2-3df83eeff2
How can I use a ruby regex or some other ruby/rails friendly method to find everything up until the first dash -
In the example above that would be: randomstring1
Thanks
You can use this pattern: ^[^\-]*
mystring = "randomstring1-randomstring2-3df83eeff2"
firstPart = mystring[0, mystring.index("-")]
Otherwise, I think the best regex is #polishchuk's.
It matches from the beginning of the string, matches as many as possible of anything that is not a dash -.
Using irb you can do this too:
>> a= "randomstring1-randomstring2-3df83eeff2"
=> "randomstring1-randomstring2-3df83eeff2"
>> a.split('-').first
=> "randomstring1"
>>
For this situation, the index solution given by agent-j is probably better. If you did want to use regular expressions, the following non-greedy (specified by the ?) regex would grab it:
(^.*?)-
You can see it in Rubular.

Resources