Ruby regex include all alphabets, numbers and / [duplicate] - ruby

This question already has answers here:
How to escape all characters with special meaning in Regex
(2 answers)
Closed 7 years ago.
I know this might be asked time and again. But I'm really stuck with this. I've got it to work for including numbers and alphabets but I have no idea on how to include "/" also.
This is what I have,
name.gsub!(/[^0-9A-Za-z]/, '')
So if name is "Cool Stuff *(#/" it returns "CoolStuff". I'd just like it to return "CoolStuff/".

The / is a special character that must be 'escaped' (meaning to take the / literally, and not for a switch or special meaning). So you have:
name.gsub!(/[^0-9A-Za-z]/, '')
But also realize you could shorten your RegEx statement by making it case insensitive by adding an 'i' after the ending slash and therefore allowing you to drop either the [A-Z] or the [a-z] part. So you could have instead:
name.gsub!(/[^0-9A-Z\/]/i, '')
Hope this helps!

Related

Regex with Ruby gsub [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
How to back reference "inner" selections ( () ) in a regular expression?
(3 answers)
Closed 4 years ago.
My goal is to replace spaces and "/" with '-' from the input:
name = "chard / pinot noir"
to get:
"chard-pinot-noir"
My first attempt is:
name.gsub(/ \/\ /, '-') #=> "chart-pinot noir"
My second attempt is:
name.gsub(/\/\s+/, '-') #=> "chard -pinot noir"
My third attempt is:
name.gsub(/\s+/, '-') #=> "chard-/-pinot-noir"
This article helped. The first group checks for a forward slash /, and contains a break. The second portion replaces a forward slash with '-'. However, the space remains. I believe /s matches spaces, but I can't get it to work while simultaneously checking for forward slash.
My question is how can I get the desired result, shown above, with varying strings using either regex or a ruby helpers. Is there a preferred way? Pro / Con ?
If you don't know much about regex, you can do this way.
name = "chard / pinot noir"
(name.split() - ["/"]).join("-")
=> "chard-pinot-noir"
I think the best way is use with regex as #Sagar Pandya described above.
name.gsub(/[\/\s]+/,'-')
=> "chard-pinot-noir"

Is there a difference between `[^\b]` and `.`? [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
\B+ vs [\B]+ vs [^\b]+ in Python regex
(2 answers)
What's the use of the [\b] backspace regex?
(3 answers)
Closed 5 years ago.
Is there a difference between [^\b] and .?
I was modifying some code created by someone else that included this no-word-boundary-character-class ([^\b]). and am not able to find a difference between that and wildcard . (this is in ruby).
My assumption was that [^\b]+ when applied to the string hello world should match hello and stop before the space, (as that is where there is a word boundary.
My observation is that it seems to just match everything. rubular link.
What should be happening here?
[\b] means backspace and [^\b] not a backspace
\b is not a character, it can't be included in a character class.
The negation of a word boundary is \B

extracting link from text [duplicate]

This question already has answers here:
How to extract URLs from text
(6 answers)
Closed 8 years ago.
I am tring to extract a link from a phrase and it could be any where last, first or middle so I am usig this regex
link=text.scan(/(^| )(http.*)($| )/)
but the problem is when the link is in the middle it gets the whole phrase until the end.
What should I do ?
It's because .* next to http is greedy. I suggest you to use lookarounds.
link=text.scan(/(?<!\S)(http\S+)(?!\S)/)
OR
link=text.scan(/(?<!\S)(http\S+)/)
Example:
> "http://bar.com foo http://bar.com bar http://bar.com".scan(/(?<!\S)http\S+(?!\S)/)
=> ["http://bar.com", "http://bar.com", "http://bar.com"]
DEMO
(?<!\S) Negative lookbehind which asserts that the match won't be preceeded by a non-space character.
http\S+ Matches the substring http plus the following one or more non-space characters.
Do all the links you are trying to match follow some simple pattern? We'd need to see more context to confidently provide a good solution to your problem.
For example, the regex:
link=text.scan(/http.*\.com/)
...might be good enough for the job (this assumes all links end in ".com"), but I can't say for sure without more information.
Or again, for example, perhaps you could use something like:
link=text.scan(/http[a-z./:]*) - this assumes all links contain only lower case letters, ".", "/" and ":".

What does this regex `str.gsub(/\#{(.*?)}/)` do? [duplicate]

This question already has answers here:
Greedy vs. Reluctant vs. Possessive Qualifiers
(7 answers)
Closed 8 years ago.
.* means any character, so why is the .*? needed in the following?
str.gsub(/\#{(.*?)}/) {eval($1)}
.* is a greedy match, whereas .*? is a non-greedy match. See this link for a quick tutorial on them. Greedy matches will match as much as they can, while non-greedy matches will match as little as they can.
In this example, the greedy variant grabs everything between the first { and the last } (the last closing brace):
'start #{this is a match}{and so is this} end'.match(/\#{(.*)}/)[1]
# => "this is a match}{and so is this"
while the non-greedy variant reads as little as it needs to make the match, so it only reads between the first { and the first successive }.
'start #{this is a match}{and so is this} end'.match(/\#{(.*?)}/)[1]
# => "this is a match"

what is the best way to remove the last n characters of a string (in Ruby)? [duplicate]

This question already has answers here:
Ruby, remove last N characters from a string?
(13 answers)
Closed 5 years ago.
in Ruby,
I just want to get rid of the last n characters of a string,
but the following doesn't work
"string"[0,-3]
nor
"string".slice(0, -3)
I'd like a clean method, not anything like
"string".chop.chop.chop
it may be trivial, please anyone teach me! thanks!
You can use ranges.
"string"[0..-4]
You could use a regex with gsub ...
"string".gsub( /.{3}$/, '' )
If you add an ! to slice it will destructively remove the last n characters without having to assign it to another variable:
my_string.slice!(my_string.length-3,my_string.length)
compared to:
new = my_string.slice(0..-4)

Resources