This is a mirror of official site: http://jasper-net.blogspot.com/

Why can't I get my regular expression pattern to match words that begin with %?

| Sunday, April 25, 2010
A customer asked for help writing a regular expression that, in the customer's words, matched the string %1 when it appeared as a standalone word.
Match No match
%1 %1b
:%1: x%1

One of the things that people often forget to do when asking a question is to describe the things that they tried and what the results were. This is important information to include, because it saves the people who try to answer the question from wasting their time repeating the things that you already tried.
Pattern String Result Expected
\b%1\b %1 No match Match
\b%1\b :%1: No match Match
\b%1\b x%1 Match No match
^..$ %1 Match Match

That last entry was just to make sure that the test app was working, a valuable step when chasing a problem: First, make sure the problem is where you think it is. If the ^..$ hadn't worked, then the problem would not have been with the regular expression but with some other part of the program.

"Is the \b operator broken?"

No, the \b operator is working just fine. The problem is that the \b operator doesn't do what you think it does.

Read more: The Old New Thing

Posted via email from jasper22's posterous

0 comments: