How do I get the location for the first matching occurance of a regex, in a string? I need an expression that will take the regex /[0-9]/, find its first occurance in a string such as "_835A_Photo_descriptor.png". The point being that sometimes the first character of the file name is a number, sometimes it is not.

index is not good because I need to match a single digit, not a particular digit, thus regex.

I've tried pos, but that does not work, because of the m/xxx/g requsite, and besides, it does not give the beginning location, only where it last looked.

I'm trying @-, per perlvar.pod, but it does not seem to work.

I need this positional value, to evaluate other parts of the string.

foreach $filename (@curdirlist) { # print "$filename\n"; #sane if ( $filename =~ /([0-9])/ ) { ($startloc = @-) && ($alphaloc = $startloc + 4); } print "$startloc "; }

All $startloc s are printing 2, which is never correct given the sample set. It should be 0 or 1 only. (?) Most files start with a digit, some do not.

I stopped learning python because in my opinion, python does not natively support regular expressions, and I got tired of seeing how python works in MSDOS, which I do not use. Most of what I've done in the past has been with bash, sed and awk. But now I need more! More power imagne:<span style="text-decoration: underline;">NOW!!!</span>

So I'm learning perl. But I feel really stupid, like I'm missing the most obvious thing, on this problem.

David


In reply to position of first matching regex by techtaskers.com

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.