Regexp.escape not escaping forward slashes? - ruby

In IRB if I pass a string like "/domain/path" to Regexp.escape it just returns it the same. I thought that forward slashes are supposed to be escaped with a backslash? Am I missing something here?

Also, the only reason why you would need to escape / characters is because it is your delimiter for the regexp, if you specify other type of delimiters (or make an instance of the Regexp class) you won't have this issue:
/^hello\/world$/ # escaping '/' just to say: "this is not the end"
%r"^hello/world$" # no need for escaping '/'
Regexp.new('^hello/world$') # no need for escaping '/'

Regexp.escape
Regexp.new(Regexp.escape('/domain/path'))
=> /\/domain\/path/
OR
Regexp.new(Regexp.escape('domain/path'))
=> /domain\/path/

Related

How to get three consecutive backslashes in Regexp

I want to create a regular expression such as:
/\\\s*\\\s*$/
I am trying it in this way:
Regexp.new('\\\s*\\\s*$') # => /\\s*\\s*$/
What am I doing wrong?
Well (\\) matches a single backslash. Backslash serves as an escape character for Regexp.
rgx = Regexp.new('\\\\\\s*\\\\\\s*$')
A more verbose way of doing this would be the following as #Cary Swoveland stated.
rgx = Regexp.new('\\{3}s*\\{3}s*$')
Using literal notation avoids some confusion. This compiles to what you said you want:
/\\\s*\\\s*$/
Though, to be clear, this still matches a single backslash, optional whitespace a single backslash and more optional whitespace. Backslashes are escaped when you inspect a regexp.

Ruby regex to remove / from string

I currently have a string to remove spaces from strings, however I now need to remove forward slashes from the string too. I'm not very good with regexes and could use some help thanks. This is the current regex I have: gsub(/\s+/, "") how do I modify this to remove / ? I've played around in the console and can't seem to get it.
You have to escape the forward slash because it's a special character. Something like this:
s = "This is a line / string"
s.gsub(/[\s\/]/, '') # => "Thisisalinestring"

How to make Ruby ignore backslash in strings?

Is there some way in Ruby that I can avoid having to put double-backslash in Ruby strings (like what can be done in C#):
For example, in C# was can prefix a string with # and then the backslash in the string does not need to be escaped:
#"C:\Windows, C:\ABC"
Without # we would need to escape the backslash:
"C:\\Windows, C:\\ABC"
Is there something similar in Ruby?
Use single quotes
my_string = 'C:\Windows'
See more in the Strings section here
You can also use %q and backslashes will be automatically escaped for you:
%q{C:\Windows} => "C:\\Windows"

Escaping \ in regular expressions in Ruby

I was parsing a file and some lines of the file ended with "\". I wanted to use gsub to find and replace it. I tried '\' and /\/ and neither one correctly matched "\".
I ended up getting around it by using a combination of chop and strip but it left me thinking how would I do this if I ever need to again?
You need to escape the escape sign as well. So this should work:
/\\/
Passing a string to gsub that will then be compiled to a regex:
"abc\def".gsub("\\", "")
=> "abcdef"
Or just providing the regex directly:
"abc\def".gsub(/\\/, "")
=> "abcdef"

Ruby RegEx problem text.gsub[^\W-], '') fails

I'm trying to learn RegEx in Ruby, based on what I'm reading in "The Rails Way". But, even this simple example has me stumped. I can't tell if it is a typo or not:
text.gsub(/\s/, "-").gsub([^\W-], '').downcase
It seems to me that this would replace all spaces with -, then anywhere a string starts with a non letter or number followed by a dash, replace that with ''. But, using irb, it fails first on ^:
syntax error, unexpected '^', expecting ']'
If I take out the ^, it fails again on the W.
>> text = "I love spaces"
=> "I love spaces"
>> text.gsub(/\s/, "-").gsub(/[^\W-]/, '').downcase
=> "--"
Missing //
Although this makes a little more sense :-)
>> text.gsub(/\s/, "-").gsub(/([^\W-])/, '\1').downcase
=> "i-love-spaces"
And this is probably what is meant
>> text.gsub(/\s/, "-").gsub(/[^\w-]/, '').downcase
=> "i-love-spaces"
\W means "not a word"
\w means "a word"
The // generate a regexp object
/[^\W-]/.class
=> Regexp
Step 1: Add this to your bookmarks. Whenever I need to look up regexes, it's my first stop
Step 2: Let's walk through your code
text.gsub(/\s/, "-")
You're calling the gsub function, and giving it 2 parameters.
The first parameter is /\s/, which is ruby for "create a new regexp containing \s (the // are like special "" for regexes).
The second parameter is the string "-".
This will therefore replace all whitespace characters with hyphens. So far, so good.
.gsub([^\W-], '').downcase
Next you call gsub again, passing it 2 parameters.
The first parameter is [^\W-]. Because we didn't quote it in forward-slashes, ruby will literally try run that code. [] creates an array, then it tries to put ^\W- into the array, which is not valid code, so it breaks.
Changing it to /[^\W-]/ gives us a valid regex.
Looking at the regex, the [] says 'match any character in this group. The group contains \W (which means non-word character) and -, so the regex should match any non-word character, or any hyphen.
As the second thing you pass to gsub is an empty string, it should end up replacing all the non-word characters and hyphens with empty string (thereby stripping them out )
.downcase
Which just converts the string to lower case.
Hope this helps :-)
You forgot the slashes. It should be /[^\W-]/
Well, .gsub(/[^\W-]/,'') says replace anything that's a not word nor a - for nothing.
You probably want
>> text.gsub(/\s/, "-").gsub(/[^\w-]/, '').downcase
=> "i-love-spaces"
Lower case \w (\W is just the opposite)
The slashes are to say that the thing between them is a regular expression, much like quotes say the thing between them is a string.

Resources