3

I'm trying the same super simple regex on Rubular.com and my VM Linux with Ruby 1.9.2 I dont' know why I'm getting different outputs: VM:

my_str = "Madam Anita"
puts my_str[/\w/]

this Outputs: Madam

on Rubular it outputs: MadamAnita Rubular: http://www.rubular.com/r/qyQipItdes

I would love some help. I stuck here. I will not be able to test my code for the hw1.

2 Answers 2

4

No, it doesn't really. It matches all characters in "Madam" and "Anita", but not the space. The problem you are having is that my_str[/\w/] only returns a single match for the given regular expression, whereas Rubular highlights all possible matches.

If you need all occurrences, you could do this:

1.9.3p194 :002 > "Madam Anita".scan(/\w+/)
 => ["Madam", "", "Anita", ""] 
Sign up to request clarification or add additional context in comments.

1 Comment

Use \w+ instead...empty groups are usually not so useful.
3

Actually, \w matches a single character. The result in Rubular contains spaces between adjacent characters to tell you this (though I wish they'd also make the highlighting more obvious...). Compare with the output from matching \w+, which matches two strings (Madam and Anita).

1 Comment

thanks for notice this out. I would like a better highlighting also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.