'John Merlino' via Ruby on Rails: Talk wrote in post #1155178:
> I thought the ? matches zero or one occurrence of a pattern. However in
> this example:
>
> def show_regexp(string, pattern)
>   match = pattern.match(string)
>   if match
>     "#{match.pre_match}->#{match[0]}<-#{match.post_match}"
>   else
>     "no match"
>   end
> end
>
> a = "The moon is made of cheese"
> show_regexp(a, /\s.*?\s/) #=> The-> moon <-is made of cheese
>
> What exactly is the ? doing?

*? -- 0 or more, lazy. Matches will be as small as possible.

/\s.*\s/.match(searchText) (not lazy)

/\s.*?\s/.match(searchText) (lazy)

/\s.*\s/ (not lazy)
The-> moon is made of <-cheese (1 match)

/\s.*?\s/ (lazy)
=> The-> moon <-is-> made <-of cheese (2 matches)

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/7d945ebed056766f94fbfde72241a560%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to