Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Regex with Backslashes

by tybalt89 (Monsignor)
on May 17, 2020 at 17:02 UTC ( [id://11116865]=note: print w/replies, xml ) Need Help??


in reply to Regex with Backslashes

Your problem is confusing single quotes and double quotes.

'\,' is exactly equal to '\\,' and so is "\\,"

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

has only one backslash before the comma and therefor should *not* split there.
my $regex = qr/(?<!\\),|(?<=\\\\),/;
is correct and should not split at the '\\,' (which is exactly equal to '\,')

I'd recommend you play around with single quotes and backslashes to understand how they work.

Replies are listed 'Best First'.
Re^2: Regex with Backslashes
by anita2R (Scribe) on May 17, 2020 at 18:57 UTC

    Thanks

    That helps my understanding of the problem and suggests to me that I am not going to be able to parse my data using a single regex

    I could change the data, perhaps using ',,' for a non-splitting comma instead of '\,' which I can't differentiate from '\\,'

      No.

      Look again at your output

      1 This is a problem->\,B 2
      this output is correct, it is *not* a problem...

      For the last part of your comment
      instead of '\,' which I can't differentiate from '\\,'
      If you would restate that as double-quoted strings it would look like this
      instead of "\\," which I can't differentiate from "\\,"
      which is true, but not what you meant, you meant to say (in double-quoted strings)
      instead of "\\," which I can't differentiate from "\\\\,"
      which you can and have done.

      In a single-quoted string, the backslash ONLY does quoting if it occurs before a ' or a \
      otherwise it stands for itself. That's why '\,' and '\\,' actually represent the same string.

      Try looking at the printed string instead of the perl form of the string to see what you actually have.

        I understand that in a single quoted string '//' is the same as '/' and looking at the printed string

        my $text = '1,This\, is a problem->\\,B,99'; print "$text\n"; > output 1,This\, is a problem->\,B,99

        I can see this.

        Whilst, as you point out, I can get a regex to work correctly, I still can't get the output that I need. '//' is the same as '/' so I am never going to be able to 'differentiate' the two, or have I still not grasped it!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found