in reply to Re^2: Extract a pattern from a string
in thread Extract a pattern from a string

This version is slightly more efficent:
perl -E 'my $s="ME170-5/2/8-ME172-2/2/6-ME4028"; while ($s=~m|(\d{1,2 +}/\d{1,2}/\d{1,2})|g){++$x;say "$x Found $1:" . " POS=" . (pos($s)-l +ength($1)+1)}'
The reason is that the 'index' operator re-scans the string, while 'pos' uses an existing value, and the 'length' computation is significantly faster than scanning.
Update: It also does not suffer from the bug ambrus (++) points out below.

             I hope life isn't a big joke, because I don't get it.
                   -SNL

Replies are listed 'Best First'.
Re^4: Extract a pattern from a string
by avim1968 (Acolyte) on Jun 11, 2012 at 05:35 UTC
    Hi
    I have made those two codes based on what you wrote.
    in the case of multiple identical port numbers in the string.
    i need to know which would be faster/wiser to use ?
    $s=ME170-5/2/8-ME172-5/2/8-ME4028ME172-5/2/8-ME196-5/2/8-ME4002; while ($s=~m/(\d{1,2}\/\d{1,2}\/\d{1,2})/g) {++$r;print "$r Found $1:" + .(pos($s)-length($1)+1) .$nl;}<br>
    _OUTPUT_
    1 Found 5/2/8:7
    2 Found 5/2/8:19
    3 Found 5/2/8:37
    4 Found 5/2/8:49
    $s=ME170-5/2/8-ME172-5/2/8-ME4028ME172-5/2/8-ME196-5/2/8-ME4002; while ($s=~m/(\d{1,2}\/\d{1,2}\/\d{1,2})/g) {++$e;print "$e Found $1:" + .($-[1]+1) .$nl;}<br>
    _OUTPUT_
    1 Found 5/2/8:7
    2 Found 5/2/8:19
    3 Found 5/2/8:37
    4 Found 5/2/8:49
    Thank you
    Avi
      Brilliant ! - $-[1] is already calculated in the regex (as @-), so yes, it is more efficient than pos+length.

                   I hope life isn't a big joke, because I don't get it.
                         -SNL

        Done THANK YOU :-)