views:

27

answers:

1

I'm trying to match parts of a larger block of content and want to only match things at the start of the string or that are preceded by a space and I only want to match things that end at the end of string or are followed by a space. Unfortunately, my regex-fu is a bit too weak to figure this out on my own. Help me Stack Overflow, you are my only hope.

Basically these should all match "the-thing-to-match";

the-thing-to-match blah
blah the-thing-to-match
blah the-thing-to-match blah
+2  A: 
/(?:^| )the-thing-to-match(?: |$)/

EDIT: Remove quantifiers.

Matthew Flaschen
I think the problem is that he won't know the actual String that he's trying to match, just the position of the word.
Serplat
No, that's right. I knew the string, I just needed to be sure it had spaces or start/end of string surrounding it.
Stephen Belanger
Ah, now that I think about it, I suppose that would make sense.
Serplat
@Matthew, you don't need *any* quantifiers on those spaces.
Alan Moore
@Alan, yeah, you're right.
Matthew Flaschen