Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-19 10:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found