Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Faster way to regex this

by hdb (Prior)
on Jun 08, 2014 at 09:39 UTC ( #1089172=note: print w/ replies, xml ) Need Help??


in reply to Faster way to regex this

Have you tried using a character class?

$dir =~ s/([\[\]{}~*?])/\\$1/g;


Comment on Re: Faster way to regex this
Download Code
Replies are listed 'Best First'.
Re^2: Faster way to regex this
by AnomalousMonk (Abbot) on Jun 08, 2014 at 11:04 UTC

    A look-ahead might be faster, but this still doesn't address the question of  \ (backslash) handling raised by Corion below.

    c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'foo\bar\[baz[x]x{x}x~x*x?x[]{}~*?'; print qq{'$s'}; ;; $s =~ s{ (?= [][{}~*?]) }{\\}xmsg; print qq{'$s'}; " 'foo\bar\[baz[x]x{x}x~x*x?x[]{}~*?' 'foo\bar\\[baz\[x\]x\{x\}x\~x\*x\?x\[\]\{\}\~\*\?'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (17)
As of 2015-07-28 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (255 votes), past polls