0

I am using Ruby 1.9.3. Just going thorugh the Ruby tutorials. Now I just got stuck to a statement on which regular expression is working and giving out put also. But confusion with the \/ operators logic.

RegExp-1

Today's date is: 1/15/2013. (String)

(?<month>\d{1,2})\/(?<day>\d{1,2})\/(?<year>\d{4}) (Expression)

RegExp-2

s = 'a' * 25 + 'd' 'a' * 4 + 'c' (String)

/(b|a+)*\/ =~ s #=> ( expression)

Now couldn't understand how \/ and =~ operator works in Ruby.

Could anyome out of here help me to understand the same?

Thanks

DoLoveSky
  • 777
  • 1
  • 6
  • 17
  • 2
    `\/` is for escaping the `/` otherwise it would close the expression early. Your last expression is invalid. Also http://stackoverflow.com/questions/5781362/ruby-operator for explaining the `=~` operator – Lee Jarvis Jan 15 '13 at 13:16
  • 2
    You're asking a question that you could answer just by trying it yourself? – Lee Jarvis Jan 15 '13 at 13:28

1 Answers1

2

\ 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.

Community
  • 1
  • 1
Eric
  • 636
  • 6
  • 16
  • Would you tell me what the output of this expression `(?\d{1,2})\/(?\d{1,2})\/(?\d{4})`? – DoLoveSky Jan 15 '13 at 13:21
  • there is no "ouput". it's just a regex (assuming you bookend it in `/`). – Eric Jan 15 '13 at 13:24
  • if you're trying to see how that regex matches the string you provided in the question, try it yourself! [rubular](http://rubular.com) is a great tool for ruby regex testing, or you can enter the ruby console and try matching the regex with the string using the =~ operator we discussed. – Eric Jan 15 '13 at 13:27
  • 1
    @DoLoveSky, Don't troll for up-votes. They will happen when people feel your contribution is worthy of it. Asking for them will irritate people causing the opposite result. Instead, contribute good, well thought-out and well-asked questions, and give solid concise answers and you'll gain votes. – the Tin Man Jan 15 '13 at 14:44
  • @DoLoveSky, did you read the answer? It says "`If there is a match, it returns the index of the start of the match`". – the Tin Man Jan 15 '13 at 21:14
  • Yes,I am now aware of that! When I asked , I was confused. After that I did some research and got the point! – DoLoveSky Jan 15 '13 at 21:17