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.
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
|
|