Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Regex with Backslashes

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


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

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.

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

    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!

      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"; }

      Outputs:

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

      Is this output a correct solution to your problem?

        Yes !!! That is a correct solution.

        I would never have thought of using a 'here document' for this.

        Now I have to decide whether to stay with my original scheme with 1,Text\, more text\\,X,99 or go with what I said in response to haukex.

      '\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:  <%-{-{-{-<

        It would be presumptuous of me to answer your question, especially as my programming skills are just at hobby level, but I recognize that backslash in a single quoted string behaves differently before a ', so you effectively lose the closing quote. I do note that print '\'' . "\n"; prints "'" which would allow you to include a single quote inside a single quoted string much as you would use \" to include double quotes in a double quoted string.

        As to my grasp of <'\x' and '\\x' produce the same string by using two different representations of the same character>, you're right!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-03-29 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found