http://qs321.pair.com?node_id=11116873


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

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!

Replies are listed 'Best First'.
Re^5: Regex with Backslashes
by tybalt89 (Monsignor) on May 18, 2020 at 00:20 UTC

    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.

Re^5: Regex with Backslashes
by AnomalousMonk (Archbishop) on May 17, 2020 at 21:17 UTC

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

        It may or may not be presumptuous of you to answer the question, but it's clear that you understand what I said you did not. Please accept my apology! :)


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