Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Regex to dereference

by Thelonius (Priest)
on Jan 18, 2007 at 23:53 UTC ( [id://595384]=note: print w/replies, xml ) Need Help??


in reply to Regex to dereference

I think this (funcb) does what you want.
#!/usr/bin/perl use warnings; use strict; my %CSV = ('n' => 'n', 't' => 't', 'f' => 'f', '\\' => '\\', ); my %CSV1 = ('\\n' => '\\n', '\\t' => '\\t', '\\f' => '\\f', '\\\\' => '\\\\', ); my @same = qw( \\ \\\\ a ab \a \a\b \a\n \n\a \n\n abc\a abc\\a ); my @diff = qw( a\\ \a\\ abc\a\\ abc\a\\\\ \a\b\c\d\e\x\g\h\\ ); print "These are the same as your function\n"; runtests(@same); print "These are different:\n"; runtests(@diff); sub runtests { for my $in (@_) { print " in: $in\n"; $_ = $in; funca(); print " funca: $_\n"; my $savea = $_; $_ = $in; funcb(); printf " funcb: %-30s %s\n\n", $_, $_ eq $savea ? "SAME" : "DIF +FERENT"; } } sub funca { my $pos = 0; while (m/\\(.)/g) { $pos=pos($_);} s/\\(.)/exists($CSV{$1}) ? "\\$1" : "\\\\$1"/ge; s/^(.{$pos})\\$/$1\\\\/; } sub funcb { my $out = ""; pos($_) = 0; s/\G(\\.|[^\\]+|\\$)/substr($1,0,1) eq '\\' ? $CSV1{$1} || "\\$1" : +$1/eg; } __END__ # output: These are the same as your function in: \ funca: \\ funcb: \\ SAME in: \\ funca: \\ funcb: \\ SAME in: a funca: a funcb: a SAME in: ab funca: ab funcb: ab SAME in: \a funca: \\a funcb: \\a SAME in: \a\b funca: \\a\\b funcb: \\a\\b SAME in: \a\n funca: \\a\n funcb: \\a\n SAME in: \n\a funca: \n\\a funcb: \n\\a SAME in: \n\n funca: \n\n funcb: \n\n SAME in: abc\a funca: abc\\a funcb: abc\\a SAME in: abc\a funca: abc\\a funcb: abc\\a SAME These are different: in: a\ funca: a\ funcb: a\\ DIFFERENT in: \a\ funca: \\a\ funcb: \\a\\ DIFFERENT in: abc\a\ funca: abc\\a\ funcb: abc\\a\\ DIFFERENT in: abc\a\\ funca: abc\\a\\\ funcb: abc\\a\\ DIFFERENT in: \a\b\c\d\e\x\g\h\ funca: \\a\\b\\c\\d\\e\\x\\g\\h\ funcb: \\a\\b\\c\\d\\e\\x\\g\\h\\ DIFFERENT NT

Replies are listed 'Best First'.
Re^2: Regex to dereference
by Hena (Friar) on Jan 19, 2007 at 06:47 UTC
    Yes. This indeed does what I wanted. Thank you :D.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-06-17 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.