uva has asked for the wisdom of the Perl Monks concerning the following question:

hi monks.. i have one doubt.In matching in perl, if i give /\w*(\w+)(\1{1,)/ , it matches for only two characters,i mean if the word is:"theeee" it searches upto "thee", what we have to do to match "theeee"

Replies are listed 'Best First'.
Re: doubt in matching
by Samy_rio (Vicar) on Feb 13, 2006 at 07:29 UTC

    Hi uva, Try this,

    $_="theeee"; if (m/\w*(\w+)(\1{1,})/si){ print $&; }

    You are missing "}" in the pattern.

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

      The /i flag only does something is you have a literal alphabetic character in the pattern. The /s only does something if you have a dot, . in the pattern. The sequence (\1{1,}) is really just \1+. :)

      $_="theeee"; if( m/\w*?(\w+)(\1+)/ ) { print "\$&: $&\n\$1: $1\n\$2: $2\n"; }

      I'm not sure what's supposed to end up where, but the pattern /\w*(\w+)(\1+)/ puts must of the stuff into the first part, \w*, because it's greedy and Perl is going to back track only as far as it needs to so it satisfies that next things. If you want more stuff in the ending parts, you have to make the first part non-greedy.

      $_="theeee"; if( m/\w*?(\w+)(\1+)/ ) { print "\$&: $&\n\$1: $1\n\$2: $2\n"; }
      --
      brian d foy <brian@stonehenge.com>
      Subscribe to The Perl Review
        thanks for the reply ,actually i want to do the following thing:: any consecutive letters in the word should be enclosed in brackets.for eg: "thhheeee good boyy" should be "t(hhh)(eeee) g(oo)d bo(yy)" . the no of consecutive letters may differ in the word.