Thanks for your reply. The data that is received consists of a single quoted string of characters. This '1,//Text//Text,C,150' is 22 characters long, as shown when I use print length($cmdValues) . "\n";, so double backslash is really two characters in the data I am processing. Later in the script if a backslash is followed by another backslash it is replaced with a single character that is displayed on an lcd screen.

The hex code was meant to mean two hex characters, rather than literally two characters, in the form 0xFF, sorry for any confusion.

I will use qr for all further work, as suggested.

As to

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

, no, the input data is

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

The expected outcome is an array containing the following:

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

The string should be split on every comma and every comma preceded by two backslash characters, but not on a comma preceded by a single backslash.

I have looked at the quote-like operators and had already been through an interesting discussion starting at Ways of quoting

..."in my $regex = '(?<!\\\),';, the string actually only contains two backslashes because '\\' becomes \ but '\)' remains as \)" ... Using Data::Dumper, I can now see (I think) why my original regex worked:

The string I was splitting looks like this

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

but when printed with Dumper it looks like this

$VAR1 = "1,This\\, is a problem->\\,B,99";

So both '\,' and '\\,' appear the same during processing. Is there a way I can stop '\,' being processed as '\\,'.

I may have to go down the route of a custom parser as you have suggested


In reply to Re^2: Regex with Backslashes by anita2R
in thread 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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.