\
serves as an escape character. In this context, it is used to indicate that the next character is a normal one and should not serve some special function. normally the /
would end the regex, as regex's are bookended by the /
. but preceding the /
with a \
basically says "i'm not telling you to end the regex when I use this /
, i want that as part of the regex."
As Lee pointed out, your second regex is invalid, specifically because you never end the regex with a proper /
. you escape the last /
so that it's just a plaintext character, so the regex is hanging. it's like doing str = "hello
.
as another example, normally ^
is used in regex to indicate the beginning of a string, but doing \^
means you just want to use the ^
character in the regex.
=~
says "does the regex match the string?" If there is a match, it returns the index of the start of the match, otherwise returns nil
. See this question for details.
EDIT: Note that the ?<month>
, ?<day>
, ?<year>
stuff is grouping. seems like you could use a bit of brush-up on regex, check out this appendix of sorts to see what all the different special characters do.