[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Questions about * + and ? in Regex
From:       MonkeeSage <MonkeeSage () gmail ! com>
Date:       2007-12-31 7:34:57
Message-ID: eae30c40-0aab-4668-94d4-b9503a46233d () e26g2000hsh ! googlegroups ! com
[Download RAW message or body]

On Dec 31, 1:24 am, Carlos Ortega <caof2...@yahoo.com> wrote:
> Thanks a lot  Chris  now I think I got it, however I still have the
> doubt interpreting this:
>
> show_regexp('hi hi hihihi hi hi', /\s.*?\s/)
>
> Overall my confusion arrives when 2 special characters are together...
>
> Cause this last would be:
> -Match a space
> -Followed by 0 or More characters
> -Followed by ..... <= Here is my doubt
> -Ending with a space.
>
> Again I would appreciate you help on this.
>
> Regards
> Carlos
>
>
>
> Chris Shea wrote:
> > On Dec 30, 2007 11:25 PM, Carlos Ortega <caof2...@yahoo.com> wrote:
> >>   else
> >> output gives:
> >>      ?  means - match 0 or 1 occurrence of  preceding expression.
> >> Posted viahttp://www.ruby-forum.com/.
>
> > A dot (.) can only match an actual character. Example 2 fails because
> > it's looking not for "0 or more occurrences of (a space  followed by
> > any character)", but "a space followed by 0 or more characters". The *
> > only applies to whatever immediately precedes it, not the whole
> > expression... unless the expression's enclosed in parentheses. A regex
> > for "0 or more occurrences of (a space  followed by any character)"
> > would be /(\s.)*/. In that case, the * applies to the parenthesized
> > group of whitespace and dot.
>
> > Example 4 fails because the only space isn't followed by anything at
> > all.
>
> > HTH,
> > Chris
>
> > P.S. I strongly recommend Jeffrey Friedl's Mastering Regular
> > Expressions.
>
> --
> Posted viahttp://www.ruby-forum.com/.

Normally "*" is "greedy" -- i.e., it matches the right-most matching
substring -- but when it's bounded by "?" it matches the left-most
(first) instance.

"Hello world, from ruby".match(/.*?\s+/)[0]
# => "Hello "

"Hello world, from ruby".match(/.*\s+/)[0]
=> "Hello world, from "

Regards,
Jordan

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic