Regex word boundary excluding the hyphen

regex

i need a regex that matches an expression ending with a word boundary, but which does not consider the hyphen as a boundary.
i.e. get all expressions matched by

type ([a-z])\b

but do not match e.g.

type a-1

to rephrase: i want an equivalent of the word boundary operator \b which instead of using the word character class [A-Za-z0-9_], uses the extended class: [A-Za-z0-9_-]

Best Answer

You can use a lookahead for this, the shortest would be to use a negative lookahead:

type ([a-z])(?![\w-])

(?![\w-]) would mean "fail the match if the next character is in \w or is a -".

Here is an option that uses a normal lookahead:

type ([a-z])(?=[^\w-]|$)

You can read (?=[^\w-]|$) as "only match if the next character is not in the character class [\w-], or this is the end of the string".

See it working: http://www.rubular.com/r/NHYhv72znm