Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: How do I avoid double substitution when replacing many patterns?

by diotalevi (Canon)
on Jan 20, 2007 at 18:04 UTC ( [id://595689]=note: print w/replies, xml ) Need Help??


in reply to Re: How do I avoid double substitution when replacing many patterns?
in thread How do I avoid double substitution when replacing many patterns?

The leading $and the middle \w are common between patterns. In fact, all that's required is to require a closing ) only when there was an opening (.

Using a less well known feature

\$ # Our '$' prefix (?: # Optionally *don't* find the opening paren. | (\() # Optionally find the opening paren ) (\w+) # The middle part is captured in $2 (?(1)\)) # Require a closing paren only if $1 matched.

Merely removing the prefix

\$ # Our '$' prefix ( # Capture into $1 (?: \w+ # Plain word. | \( \w+ \) # A word with parentheses around it. ) )

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^3: How do I avoid double substitution when replacing many patterns?
by japhy (Canon) on Jan 20, 2007 at 18:18 UTC
    You could change
    (?: | ( \( ) )
    to
    ( \( )?
    ... unless you get queasy when you see quantifiers placed on capturing groups.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

      I thought about it but decided I wasn't sure whether that would work correctly with the later conditional. If it does, great.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found