Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Using variables in RegEx

by EvanK (Chaplain)
on Aug 01, 2008 at 21:47 UTC ( [id://701777]=note: print w/replies, xml ) Need Help??


in reply to Re: Using variables in RegEx
in thread Using variables in RegEx

Actually, what might serve the OP's purpose is Named backreferences, a new feature of 5.10. For example, to primitively rearrange a date:
$date = '8/1/2008'; $date =~ s!(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{4})!$+{year}-$ ++{month}-$+{day}!;
This allows you to attach a name to a backreference like (?<name>pattern) that can be used within the match as \g{name} and anywhere else (in the replace or outside of the regex) as part of the hash %+. Keep in mind though, just because its a shiny new feature doesn't mean you have to use it. First figure out if its the best tool for the job :)

Disclaimer: Of course, the example above is a quick and dirty example, and as such should never be used. It was only meant to illustrate the feature noted.

Update: Ah, perhaps I did misunderstand the original question after all.

__________
Systems development is like banging your head against a wall...
It's usually very painful, but if you're persistent, you'll get through it.

Replies are listed 'Best First'.
Re^3: Using variables in RegEx
by ikegami (Patriarch) on Aug 01, 2008 at 22:43 UTC
    I don't see how named references allow $sentenceCount++ to be executed in the replace expression.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://701777]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-18 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found