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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.