I have a question that's making me feel like a newbie, but I'm stumped and now seeking wisdom.
I'm trying to transform a lot of text using a steam of tokens. In doing so I have to I tokenize a string using a single regular expression based on concatenation of smaller regular expressions in order to strip the first/next token in the string and pass it to a handler. Here is a simplified version of what I mean...
$regex = '\w+|\d+|\s+|.*?'; $text = 'The world is foo 2!'; while ($text=~s/^$regex//) { print "token: $1\n"; }
What I'm stumped on is the *best way* to determine what part of the regular expression the current token matched -- thereby telling me the type of token and which handler I should pass it to.
I'm trying to refrain from using ?{ } to set a key I can use to call the handler. I've had my share of issues with scripts that have used construct. Doing a second match seems rather inefficient also. I'm hoping there is another solution that I'm just not grasping. Can anyone offer any suggestions that may lift the veil of igornance from my eyes?In reply to Determing what part of a regex matched. by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |