TIMTOWTDI...
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11162630 use warnings; $SIG{__WARN__} = sub { die @_ }; use v5.12; use Test::More; say "Version: $]"; sub test { my ($msg, $str, $exp) = @_; my $pos = index($str, "*"); die "NO POINTER <$msg> in <$str>" if $pos == -1; substr($str,$pos,1) = ""; pos($str) = $pos; die "MULTIPLE POINTERS <$msg> in <$str>" if index($str,"*") >-1; my ($match) = $str =~ /(?|(?=(\d+))\d*\G\d|(\z))/; is ($match,$exp,$msg); } test "In the middle", 'World 12*3456 Hello 789 ' => "123456" ; test "At the start" , 'World *123456 Hello 789 ' => "123456" ; test "At the end" , 'World 12345*6 Hello 789 ' => "123456" ; test "Before" , 'Wo*rld 123456 Hello 789 ' => "" ; test "Right before" , 'World* 123456 Hello 789 ' => "" ; test "After" , 'World 123456 *Hello 789 ' => "" ; test "Right after" , 'World 123456* Hello 789 ' => "" ; test "In next Nr" , 'World 123456 Hello 7*89 ' => "789" ; test "Nr at start" , '*123456 Hello' => "123456" ; test "Nr at end" , 'World 12345*6' => "123456" ; test "Pos at end" , 'World 123456*' => "" ; done_testing;
Outputs:
Version: 5.040000 ok 1 - In the middle ok 2 - At the start ok 3 - At the end ok 4 - Before ok 5 - Right before ok 6 - After ok 7 - Right after ok 8 - In next Nr ok 9 - Nr at start ok 10 - Nr at end ok 11 - Pos at end 1..11
In reply to Re: How to capture backwards using regex?
by tybalt89
in thread How to capture backwards using regex?
by harangzsolt33
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |