I am learning Ruby and I have something to match with (/^1\/1. Guess a word from an anagram [RUBY]{4}$/
)
Please, what does "1\/1
." mean in this expression. Can anyone explain what's going on for me.
Thanks
I am learning Ruby and I have something to match with (/^1\/1. Guess a word from an anagram [RUBY]{4}$/
)
Please, what does "1\/1
." mean in this expression. Can anyone explain what's going on for me.
Thanks
Generally speaking, a backslash in a regular expression escapes the next character, so that it's treated as an ordinary character rather than whatever its special meaning would be. For instance a*
matches zero or more of the letter a
, but a\*
matches, literally, an a
followed by a star. Since most regular expressions in Ruby are wrapped in the delimiter /
, we can't directly put forward slashes in our regex. If we had written
/^1/1. Guess a word from an anagram [RUBY]{4}$/
Then the regex would be /^1/
and the rest of the line would be a very confusing syntax error. This is for the same reasons that we can't put "
characters directly inside of a "
-delimited string.
So a backslash treats it as an actual slash in the expression rather than a delimiter.
/^1\/1. Guess a word from an anagram [RUBY]{4}$/
We're literally matches a 1
followed by a slash followed by a 1
at the start of the line.