tags:

views:

99

answers:

3

Hi, so I know \bBlah\b will match a whole Blah, however it will also match Blah in "Blah.jpg" I don't want it to. I want to match only whole words with a space on either side.

A: 

Try \sBlah\s — that will match any form of whitespace on either side.

VoteyDisciple
thanks! this works. I missed that.
iamnotmad
See http://www.rubular.com/r/22vteB0NjK ; this may miss some matches, depending on what is desired.
polygenelubricants
+3  A: 

You can try: \sBlah\s.

Or if you allow beginning and end anchors, (^|\s)Blah(\s|$)

This will match "Blah" by itself, or each Blah in "Blah and Blah"

See also


Lookahead variant

If you want to match both Blah in "Blah Blah", then since the one space is "shared" between the two occurrences, you must use assertions. Something like:

(^|\s)Blah(?=\s|$)

See also


Capturing only Blah

The above regex would also match the leading whitespace.

If you want only Blah, ideally, lookbehind would've been nice:

(?<=^|\s)Blah(?=\s|$)

But since Javascript doesn't support it, you can instead write:

(?:^|\s)(Blah)(?=\s|$)

Now Blah would be captured in \1, with no leading whitespace.

See also

polygenelubricants
Rubular: http://www.rubular.com/r/jwI6eyyWpq
polygenelubricants
A: 

(^|\s)Blah(\s|$) should work, however it will also select the spaces, if you just want the word you can do this: (^|\s)(Blah)(\s|$) and take group 2 ($2 in ruby).

If want help with a RegEx, checkout: http://www.gskinner.com/RegExr/

Anlek
This may miss some matches, e.g. the last `Blah` here: http://www.rubular.com/r/xSCyF42ymy
polygenelubricants
You're correct, see commend above, to fix that, or end it with (?=\s|$)
Anlek