tags:

views:

897

answers:

5

Hello. I don't usually post to forums, but I am somewhat stuck. I am trying to find all matches in a string that begins with "| |" (quotations not included). I have tried doing:

if ($line =~ m/^\\\|\s\\\|/)

and that didn't work. I'm not sure if anyone has any tips or pointers, but any help would be appreciated. Thanks!

+16  A: 

You are escaping the pipe one time too many, effectively escaping the backslash instead.

print "YES!" if ($line =~ m/^\|\s\|/);
Vinko Vrsalovic
A: 

What about:

m/^\|\s*\|/
Zsolt Botykai
Thanks. I had tried that before, but it doesn't seem to return a match. This is what brought me to trying the double backslash. Thank you for the help.
The \s* will match zero or more whitespace characters when, according to the question, he wants to match exactly one whitespace character.
David Precious
+2  A: 

Pipe character should be escaped with a single backslash in a Perl regex. (Perl regexes are a bit different from POSIX regexes. If you're using this in, say, grep, things would be a bit different.) If you're specifically looking for a space between them, then use an unescaped space. They're perfectly acceptable in a Perl regex. Here's a brief test program:

my @lines = <DATA>;

for (@lines) {
    print if /^\| \|/;
}

__DATA__  
| | Good - space  
|| Bad - no space  
|   | Bad - tab  
 | | Bad - beginning space  
        Bad - no bars
+2  A: 
Kyle
Your solution only works if the whitespace is a space character. It fails when it is any other whitespace, such as a tab character.
brian d foy
A: 

Remove the ^ and the double back-slashes. The ^ forces the string to be at the beginning of the string. Since you're looking for all matches in one string, that's probably not what you want.

m/\|\s\|/
Mathieu Longtin