1
votes

I've seen another topic about word boundary with special characters, but I still can't fix my problem.

I wanna match this:

@_-amazingword_-

but not match this:

@_-amazingword_--

Is there a regular expression for that?

Keep in mind though, that these words can be placed in the middle of a text, for instance:

This is an @_-amazingword_- everybody.
1
Try lookahead: @_-amazingword_-(?!-) OR @_-amazingword_-(?=\s|$) - anubhava
Yeah, use (?<!\S)@_-amazingword_-(?!\S) if your regex flavor is not JavaScript. - Wiktor Stribiżew

1 Answers

3
votes

Word boundary is not going to help you, because the neither characters @ nor - are "word" characters.

Instead, use look arounds:

(?<=\s)@_-amazingword_-(?=\s)

That look behind and look ahead assert (but don't match) that the previous/next char is a whitespace character.

If you want this to work at the start/end of input as well, make the look arounds match on start/end too:

(?<=^|\s)@_-amazingword_-(?=\s|$)