Greetings O Wise Ones

I'm having a problem getting a good regular expression for this problem

For any given string, I would like to match on a string contains the sequence of characters "\n","\f","\t" or "\r" e.g. match on $str = "Test\n" but not on $str = "Test".

So my code looks like this ...

print $str."\n<br />"; if ( ($str =~ /\\r/) or ($str =~ /\\n/) or ($str =~ /\\f/) or ($str +=~ /\\t/) ) { ##0 print "found ws using multiple regexps and or with double backslas +hes\n<br />"; } if ($str =~ /\\r|\\n|\\t|\\f/) { print "matches on ws using simple ' +|' list with double backslashes\n<br />";} ## 1 if ($str =~ /[\\r,\\n,\\t,\\f]/) { print "matches in character clas +s\n<br />";} ##2 if ($str =~ /\r|\n|\t|\f/) { print "matches on single backslash\n<br + />";} ##3 if ( ($str =~ /\\n/) ) { print "matches \\\\n (with double backslash +)\n<br />";} ##4 if ($str =~/\s/) { print "matches on \\s \n<br />";}##5

and I get the following results

rrrrrrrrrr matches in character class
rrrrrrrr\nrr found ws using multiple regexps and or with double backslashes matches on ws using simple '|' list with double backslashes matches in character class matches \\n (with double backslash)
rrrrrrrr\rr found ws using multiple regexps and or with double backslashes matches on ws using simple '|' list with double backslashes matches in character class

Regexp 0 and 1 appear to work but is there a better or more succint way to do it?

My 'character class' regexp 2, doesn't work. It matches on a plain 'r'. I've obviously go tsomething wrong here but I have no idea what. If anyone can enlighten me I'd be glad to learn.

I believe regexp 3, with single backslashes doesn't work because it is looking for an actual whitespace character such as 0x0a or 0x0d and not a character string representing a whitespace character

Regexp 4 isn't really completing the task so it doesn't appear when I use \r but does when I use \n.

I think regexp 5 fails for the same reasons regexp No 3 fails

Is there a character class to deal with this regexp problem and/or a more succint way of expressing the regexp?


In reply to How to match a string containing whitespace characters by LesleyB

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.