Perl and Regular Expression Repetition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Akino877
    New Member
    • Nov 2007
    • 37

    Perl and Regular Expression Repetition

    Hello,

    I read on the web that the regular expression /o{2,3}/ means that it would match if o repeats at least 2 times but not more than 3 times. But when I tested this RE
    with "halloooo", it returned a match.

    I wonder if someone could please help me understand why it returned a match when there was 4 occurences of o?

    Thank you,

    Akino
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Because the o does repeat 3 times. It doesn't care about the surrounding characters, no matter what they happen to be. If you want it to match 2-3 consecutive o's and no more than that, you need to bookend the expression with a non-o match.

    Something like /[^o]o{2,3}[^o]/

    Comment

    Working...