Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: split on delimiter unless escaped

by yrp001 (Initiate)
on Nov 09, 2010 at 22:21 UTC ( [id://870419]=note: print w/replies, xml ) Need Help??


in reply to Re: split on delimiter unless escaped
in thread split on delimiter unless escaped

Ah, neat. I made a couple of small modification, and now I'm very close. The trouble left now is how to capture an empty field - i.e. where I have two delimiter characters next to each other I should emit an empty chunk, instead of no chunk. Still stuck on that. What I have so far:

my $re = qr{ (?> # don't backtrack into this group !! # either the escape character, # followed by an escape character | # or !; # escape followed by delimiter char | # or [^;\n] # a character that is neither delimiter # character or a newline )+ }x; while(<>) { chomp; $str = $_; print "$_\n"; while ($str =~ /($re)/g) { print " Chunk '$1' => "; $s = $1; $s =~ s/!!(?=(!|;))/!/g; print "$s\n"; } }

Example of paired delimiters (;;)

a!!val!!;;bv!!al;; Chunk 'a!!val!!' => a!!val!! Chunk 'bv!!al' => bv!!al

Replies are listed 'Best First'.
Re^3: split on delimiter unless escaped
by yrp001 (Initiate) on Nov 10, 2010 at 00:58 UTC

    So the following seems to do exactly what I want, but doesn't handle empty fields. It might not matter, because my input shouldn't have any empty fields. I'll probably just check that my input string doesn't begin or end with a delimiter, or have two consecutive delimiters in the middle anywhere. If it does, it's bad input, and I can just throw it out. Would still be fun to know how to handle empty fields, though...

    my $re = qr{ (?> # don't backtrack into this group !! # either the escape character, # followed by an escape character | # or !; # escape followed by delimiter char | # or [^;\n] # a character that is neither delimiter # character or a newline )+ }x; while(<>) { chomp; my @aray; $str = $_; print "$_\n "; while ($str =~ /($re)/g) { $s = $1; $s =~ s/!!(?=(!|;|\z))/!/g; push( @aray, $s ); } print join(' | ', @aray) . "\n"; }

Log In?
Username:
Password:

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

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

    No recent polls found