Rather than regexes, you could also use the index, split and substr built-ins, as shown in the following session under the debugger:
DB<1> $str = "this is a test"; DB<2> $sub = "is a test"; DB<3> $index = index $str, $sub; DB<4> $nr_before = scalar split /\s+/, substr $str, 0, $index; DB<5> p $nr_before; 1 DB<6> $nr_in = scalar split /\s+/, $sub ; DB<7> p $nr_in; 3 DB<8> print ++$nr_before, " " for 1..$nr_in; 2 3 4
Note: I know that the call to the scalar built-in is not really useful in a scalar context, but I thought it would clarify the idea.

Je suis Charlie.

In reply to Re: Find the position of substring by Laurent_R
in thread Find the position of substring by Anonymous Monk

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.