in reply to Re: doubt in matching
in thread doubt in matching

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

Replies are listed 'Best First'.
Re^3: doubt in matching
by uva (Sexton) on Feb 13, 2006 at 09:20 UTC
    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.

      uva, Try the below code,

      use strict; use warnings; $_="thhheeee good boyy"; s/((\w)\2{1,})/\($1\)/gsi; print $_; __END__ t(hhh)(eeee) g(oo)d bo(yy)

      Regards,
      Velusamy R.


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

      "thhheeee good boyy" should be "t(hhh)(eeee) g(oo)d bo(yy)"
      In that case, maybe you want something like this:
      #!/usr/bin/perl -w use strict; while (<DATA>) { chomp; $_ =~ s/(\w)(\1+)/\($1$2\)/g; print "$_\n"; } __DATA__ thhheeee good boyy the qqquick brrrowwwn fooox
      Output:
      t(hhh)(eeee) g(oo)d bo(yy) the (qqq)uick b(rrr)o(www)n f(ooo)x
      Cheers,
      Darren :)