in reply to Regular Expresson How to Match

In what way was the output different from your expected output? When I test the code, the output is exactly as you posted your expected output.

However, just because your snippet produced the expected output in the test case you provided doesn't mean the regexp is free of bugs. The '|' character has special meaning within regular expressions. It means "alternation" or "or". Only if you preceed it with a backslash, as in '\|' will it be interpreted as a plain old character within a regular expression:

$line =~ /^\|\s$str\s/

...would probably be closer to your goal.

You'll be much more productive with regular expressions if you take an hour to read through perlretut and perlre. There's a mountain of information to learn, but those documents provide a great start.


Dave