in reply to How to capture backwards using regex?
You can set pos of the string to continue matching, and then capture the digits to the left and right of the continuation point (\G):
#!/usr/bin/perl use 5.020; use experimental 'signatures'; use Test::More; sub digits( $S, $pos ) { pos($S) = $pos; return ($S =~ m/\b(\d*\G\d*)\b/c) ? $1 : undef; } is digits( 'Hello World 123456 !!! 789 ...', 14 ), '123456', "In middl +e"; is digits( '12345 World 123456 !!! 789 ...', 14 ), '123456', "No digit +s before"; is digits( '12345 World 123456 !!! 789 ...', 17 ), '123456', "At end"; is digits( '12345 World 123456 !!! 789 ...', 12 ), '123456', "At start +"; done_testing;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How to capture backwards using regex?
by harangzsolt33 (Deacon) on Nov 10, 2024 at 16:28 UTC |