location of regular expression match

B

bob

I apologize for another newbie question. How does one obtain the
starting character position of all regular expression matches in a
line or a paragraph?
Thank you.
 
A

Amir Kadic

bob said:
I apologize for another newbie question. How does one obtain the
starting character position of all regular expression matches in a
line or a paragraph?

One way I can think of is using pos() in a loop:


foreach $token (qw#foo bar#) {
$position= pos($line),"\n" if $line =~ /$token/g;
# now do something with it
}

I _hope_ there is a more elegant WTDI.

Amir
 
A

Amir Kadic

I said:
$position= pos($line),"\n" if $line =~ /$token/g;

Please forget the q(,"\n")

This was originally a print(), but then I thought,
'that man wants to _obtain_, not print the positions'...:)

Amir
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top