http://qs321.pair.com?node_id=687572


in reply to Regular expression * vs +

In addition to moritz' hint, you could look into the matching process by issuing (e.g.):

... my $txt = '-UK 123 123-UK 123-UK 123-UK'; print "|$1|\n" while $txt =~ /((?:\d+-UK\W?)*)/g; ...

Regards

mwa

Replies are listed 'Best First'.
Re^2: Regular expression * vs +
by ww (Archbishop) on May 20, 2008 at 11:58 UTC

    Close, but not sure OP wants this (from mwah's example):

    687551.pl syntax OK perl 687551.pl || || || || || || || || |123-UK 123-UK 123-UK| ||

    Whereas:

    my $txt = '-UK 123 123-UK 123-UK 123-UK abc-oops!'; print "|$1|\n" while $txt =~ /((?:\d+-UK\s))/g;

    produces

    perl 687551.pl |123-UK | |123-UK | |123-UK |

      I think the OP struggled over the difference between

      ... my $txt='-UK 123 123-UK 123-UK 123-UK'; print "|$1| - match until pos:" . pos($txt) . "\n" while $txt=~/((?:\ +d+-UK\W?)+)/g ; ...

      and

      ... my $txt='-UK 123 123-UK 123-UK 123-UK'; print "|$1| - match until pos:" . pos($txt) . "\n" while $txt=~/((?:\ +d+-UK\W?)*)/g ; ...

      which is the difference between (expr)+ and (expr)*, the latter matching everywhere and (of course) at 'expr', the former matching only at 'expr'.

      (maybe I misinterpreted his intention)

      Regards

      mwa