Is there a pure regex split of a string containing escape sequences?

If Perl supported variable-width look-behind assertions, you might be able to do it with something like this: split(/(?It's possible that some regex guru could come up with something that would actually work for you, but personally I'd say a finite state machine (looping through $psv a character at a time) might be a better option Something else I suppose you could try is to just split the string on the pipe character, and then check each element of the resulting list to see if it ends with an odd number of backslashes. If it does, join it back to the next element of the list with between them. Basically you'd be doing the split ignoring the escape sequences, then going back and accounting for the escapes afterwards.

If Perl supported variable-width look-behind assertions, you might be able to do it with something like this: split(/(?It's possible that some regex guru could come up with something that would actually work for you, but personally I'd say a finite state machine (looping through $psv a character at a time) might be a better option. Something else I suppose you could try is to just split the string on the pipe character, and then check each element of the resulting list to see if it ends with an odd number of backslashes. If it does, join it back to the next element of the list with | between them.

Basically you'd be doing the split ignoring the escape sequences, then going back and accounting for the escapes afterwards.

I suppose "no, but here is what such a regex would look like given one additional feature" will have to do. – Richard Simões Jul 13 '10 at 21:40 You can do this with an approach that alternates lookbehinds, accepting on even-numbered escapes and rejecting on odd-. – tchrist Nov 7 '10 at 1:33.

(unless this question was more of a mental challenge and less of a practical problem, of course). A proper way to handle X-separated data in real code is by using a proper parser - a very common one is Text::CSV_XS (don't let the name fool you - it can handle any separator characters, not just commas). It will handle escapes correctly, along with quoting.

You don't need to use split for this task. An alternative is: my $psv = "aaa|bbb||ccc|\\|\\|\\||\\\\\\\\\\\\"; print "$psv\n"; my @words = map { s/\\(\\|)/$1/g; $_; } ($psv =~ /(?:^|\|) ((?:\\\\| | ^|)*)/gx); printf("%s\n", join(", ", @words)); The regular expression may look scary, but is easily explained. It matches each of the words which are separated by the pipes.It starts either at the beginning of the string or a pipe separator.

Then follows an arbitrary number of either an escape sequence (\ + one of \|) or an arbitrary character except pipe. The regular expression inside the map just replaces the escape sequences with what they really mean.

I didn't see your solution, but we have similar Regex's. Some credit to ikegami @ perlmonks. Org for regex debugging.

– vol7ron Jul 9 '10 at 6:58.

Sweeter Solution This method does not use split, but does use a simple regex. #! /usr/bin/perl -w use strict; sub main{ (my $psv = ) =~ s/\s+$//s; my @arr = $psv =~ /(?:^|\G\|)((?:^\\||\\.)*)/sg; { local $" = ', '; # $" - sets the pretty print print "@arr \n"; # outputs: abc, def, g\|i, jkl, m\|o, pqr, s\\u, v\w, x\\, , z } } main(); __DATA__ abc|def|g\|i|jkl|m\|o|pqr|s\\u|v\w|x\\||z.

You don't need to use split for this task. The regular expression may look scary, but is easily explained. It matches each of the words which are separated by the pipes.

It starts either at the beginning of the string or a pipe separator. Then follows an arbitrary number of either an escape sequence (\ + one of \|) or an arbitrary character except pipe. The regular expression inside the map just replaces the escape sequences with what they really mean.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions