RegEx word boundaries

Recently I was looking for the word "fail" or "pass" in a string so I had the following regEx set up:

view plain print about
1#reFindNoCase("pass|fail", status)#

Which worked fine until testing came along and "failed" got passed through successfully.

To prevent this you need to use regex boundaries, "\b", so my new statement becomes:

view plain print about
1#reFindNoCase("\bpass\b|\bfail\b", status)#

In simple terms "\b" perform a "whole word only" search means that it trapped 'failed', 'failure' you get the point.

Posted: 15-Feb-2007

View: 4726

Permalink: here

Comments

I'm sure you know this, but this won't happen if you use isValid():

isValid("regex","failure","(fail|pass)")

#1 Per
15/Feb/07 5:16 PM

You can of course add some readability by grouping the two and only having a single start and end boundary: "\b(pass|fail)\b"


Pex: I think this is because the IsValid is testing whether the RegEx matches the entire string? (equivalent to "^(pass|fail)$")
Whether that's desirable or not would depend on what status contains (just a single word, or a sentence)


Probably worth noting also that \b is a zero-width match - it doesn't occupy any character spaces. (unlike, for example, "[^a-z]fail[^a-z]" which would fail if the source text was just "fail" as the two sets either side are both single-width matches.)

#2 Peter Boughton
16/Feb/07 12:17 AM

Interesting, but I have a different problem, how can you add a word match inside the brackets ?

#3 sanjuro
28/Feb/08 5:36 AM