5

Thanks in advance...

I am having some trouble with regular expressions in ruby, or otherwise finding a way to remove a slash from a string. Here is how my string looks:

string = "word \/ word"

I am trying to remove both the backslash and the slash; I want this result:

string = "word  word"

I think I am missing something with escape characters, or who knows what!

I have tried this:

string.gsub(/\//, "")

which will remove the backslash, but leaves the slash. I have tried variations with escape characters all over and in places that don't even make sense!

I am terrible with regex and get very frustrated working with strings in general, and I am just at a loss. I'm sure it's something obvious, but what am I missing?

tchrist
  • 78,834
  • 30
  • 123
  • 180
Jacob Terry
  • 209
  • 1
  • 3
  • 6
  • `string.gsub(/\/|\\/,"")` will remove both slashes, but as Niklas B points out, you are better using string replace. – Alan Apr 01 '12 at 00:47
  • there's actually only one slash in the string because the first one is an unnecessary escape slash – pguardiario Apr 01 '12 at 06:01
  • Not an actual answer, but you can use alternate regex syntax, something like `string.gsub(%r!\\/!, '')` to ease the pain of the 'how many times do i escape this?' question. – x1a4 Apr 01 '12 at 06:08
  • 3
    Am I missing something? Where is Niklas B answer? – ismail Apr 11 '13 at 10:51
  • 1
    doesnt work on this one: a = "/a/\a\a\\/a\a/a\/a/\a/\a" – meso_2600 Jul 12 '14 at 23:34

3 Answers3

5

The reason why is because both / and \ are not valid characters in a Regexp on their own. So they must be escaped by putting a \ before them. So \ becomes \\ and / become \/. Putting these together inside another set of slashes to make a Regexp literal, we get:

string.gsub(/\\\//, "")

Another way to write this is:

string.gsub(/#{Regexp.escape('\/')}/, "")

You should check out rubular for a nice way to develop Regexp strings.

http://rubular.com/r/ml1a9Egv4B

Grant Hutchins
  • 4,275
  • 1
  • 27
  • 32
  • However, I prefer Niklas B.'s solution below since it doesn't even use a Regexp. – Grant Hutchins Apr 01 '12 at 00:48
  • You can write the regexp without interpolation: `string.gsub(Regexp.escape("\/"), '')`. – steenslag Apr 01 '12 at 07:05
  • Actually that doesn't do the same thing, @steenslag. First off, I want to point out I had an error in my answer (which I've edited). The string `'\/'` needs to be in single quotes so that Ruby doesn't unescape it to `/`. Next, `Regexp.escape` returns a string, so your example is just doing this: `string.gsub('/', '')` which is not what you intended. Instead, you should either directly supply a string, or use `Regexp.escape` interpolated inside an actual Regexp. Hopefully that all made sense! – Grant Hutchins Apr 19 '12 at 00:09
3
str = "word \/ word"
p str.delete('\/') #=>"word  word"
# to get rid of the double spaces:
p str.delete('\/').squeeze(' ') #=>"word word"
steenslag
  • 79,051
  • 16
  • 138
  • 171
0

It actually does what you want but not for the reasons you think:

string = "word \/ word"
# => "word / word"
string.gsub(/\//, "")
# => "word  word"

Note: you need gsub! if you want to replace the contents of string

pguardiario
  • 53,827
  • 19
  • 119
  • 159