Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: better way to escape escapes (updated)

by LanX (Canon)
on Apr 16, 2014 at 01:09 UTC ( #1082413=note: print w/ replies, xml ) Need Help??


in reply to better way to escape escapes

Would be easier if you provided sample data.

The basic idea is to use or conditions, which match first an escaped sequence, secondly a directive and then a single character.

With a /g modifier within a while condition the match will start where the last one ended.

Group only on your directives.

update

2 proofs of concept:

The second one w/o while loop.

Please notice that you need to check for defined, since every escape or single character is an empty (since ungrouped) match.

DB<120> $str='xxx@@xx\@xx@abc\@xxx@efg@\xxx' => "xxx\@\@xx\\\@xx\@abc\\\@xxx\@efg\@\\xxx" DB<121> print ( defined $1 ? "$1\t" : "") while ( $str =~ m/ (?: [\\ +\@]{2} | ( \@\w+ ) | . ) /xg ) @abc @efg DB<122> grep { defined } $str =~ m/ (?: [\\\@]{2} | ( \@\w+ ) | . ) +/xg => ("\@abc", "\@efg")

update

more efficient:

DB<135> grep {defined} $str =~ m/ (?: [\\\@]{2}+ | ( [\\\@]\w+ ) | [ +^\\\@]+ ) /xg => ("\@abc", "\@efg")

Cheers Rolf

( addicted to the Perl Programming Language)


Comment on Re: better way to escape escapes (updated)
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2014-07-29 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls