Keep It Simple, Stupid PerlMonks

### Re^2: Regex with Backslashes

by anita2R (Scribe)
 on May 17, 2020 at 18:57 UTC Need Help??

in reply to Re: Regex with Backslashes
in thread Regex with Backslashes

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 '\\,'

Replies are listed 'Best First'.
Re^3: Regex with Backslashes
by tybalt89 (Prior) on May 17, 2020 at 19:35 UTC

No.

Look again at your output

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

For the last part of your comment
instead of '\,' which I can't differentiate from '\\,'
[download]
If you would restate that as double-quoted strings it would look like this
instead of "\\," which I can't differentiate from "\\,"
[download]
which is true, but not what you meant, you meant to say (in double-quoted strings)
instead of "\\," which I can't differentiate from "\\\\,"
[download]
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
[download]

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!

It's a META-LANGUAGE PROBLEM!! Everyone DUCK!!! ............................ quack, quack

Here's a third way of quoting that, unlike ' and " completely disables the special properties of \.
It's a version of a "here-document" and it's documented in perlop.

#!/usr/bin/perl

use strict; # https://perlmonks.org/?node_id=11116857
use warnings;

my $text = <<'END'; # this quoting method disables special properties +of \ 1,Something\,\\text\\text\0x2B,X,99,\,\\,\\\,foobar END chomp$text;

print "input:\n$text\n"; my @fields = split /(?<!\\),|(?<=\\\\),/,$text;
print "\noutput:\n";
for ( @fields )
{
print "\$_\n";
}
[download]

Outputs:

input:
1,Something\,\\text\\text\0x2B,X,99,\,\\,\\\,foobar

output:
1
Something\,\\text\\text\0x2B
X
99
\,\\
\\\
foobar
[download]

Is this output a correct solution to your problem?

'\x' and '\\x' produce the same string by using two different representations of the same character. That's the point you have not grasped. (Update: I couldn't use  '\' as an example because it's illegal! Why?)

Give a man a fish:  <%-{-{-{-<

Log In?
 Username: Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2020-11-23 17:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?