Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Greetings Monks,

I am trying to construct a regex to use in a split command.

The string to be split consists of from one to four comma separated fields. The second field can contain text which may use backslashes to escape non-separating commas '\,', the backslash itself '\\', and 2 character hex codes '\x2B' hex codes '\0x2B'. Here is an example:

$text = '1,Something\,\\text\\text\0x2B,X,99';

I can split this with a regex using a negative lookbehind:

my $regex = '(?<!\\\),'; split( /$regex/, "$text" );

so it splits on a comma unless it is preceded by a backslash.

Actual outcome:

1 Something\,\text\text\0x2B X 99

(In a terminal the double backslash appears as a single \, but this is not an issue as the split data is processed further in the perl script).

Now my problem:

If an escaped backslash comes before the next separating comma the regex 'sees' a backslash before the separating comma and does not split.

$text = '1,This is a problem->\\,B,2';

Actual output:

1 This is a problem->\,B 2

I have tried several regexe's based on the concept that there needs to be a match on a comma except when preceded by a backslash, using negative lookbehind OR there is a match on a comma when preceded by two backslashes using positive lookbehind. Here are two that I have tried

$regex = '(?<!\\\),|(?<=\\\\),';

$regex = '(?<!\\\),|(?<=[\\\]{2}),';

Neither gives the required output - so maybe I am lost in ever more escaped escaped escaped backslashes! I have tried variations with more backslashes in the positive lookbehind section of the regex. I also tried \Q...\E to avoid escaping the backslashes but this results in an error:'Unrecognized escape \Q passed through in regex'. I tried building the regex with qr like this

my $regex = qr /(?<!\\),|(?<=\\\\),/;

but it still didn't split on '\\,'.

As a test of my concept I replaced all backslashes with colons

my $regex = '(?<!:),|(?<=[:]{2}),'; my $text = '1,This:, is not a problem->::,B,2'; my @test = split( /$regex/, "$text" ); foreach( @test ) { print "$_\n"; }

The output was 'correct'

1 This:, is not a problem->:: B 2

In summary: I need to split a string at each comma or comma preceded by two backslashes but don't split at a comma preceded by only one backslash.

Any suggested approaches to this problem would be appreciated.


In reply to Regex with Backslashes by anita2R

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-16 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found