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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |