my $string = "a *test* b";
With the regexp, /\b\*test\*\b/ you get a failure because the junction between a space character and an asterisk (\*) is not a word boundry, because neither space nor \* is a word character (\w). You need to rearrange the regexp one of a couple of ways, depending on what you're trying to accomplish:
However, the second example doesn't need the \b, because it is implicit in the fact that you're already specifying non-word characters (*) that must preceed and follow the literal word characters of 'test'.m/\b\s\*test\*\s\b/; or m/\*\btest\b\*/;
The first example (probably what you really should be using) says, match where a word has just ended, followed by a space, followed by *test*, followed by a space, followed by a word beginning. That's what the \b is doing for you. Of course \b is a zero width assertion, so you haven't actually matched what comes before the first \b or what comes after the second \b, you're just asserting that there must be a word there.
Note, I used \s to indicate space, but if you want to use an actual space (ascii 32), put a space in your regexp, or spell it out with its ascii value. I used \s so that the whitespace would be clearly visible in reading the regexp.
Hope this helps...
Dave
"If I had my life to do over again, I'd be a plumber." -- Albert Einstein
In reply to Re: regex question
by davido
in thread regex question
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |