Regex - Match substring outside of hashtags(#)

Viewed 53

Taking this string as an example:

focus;focus#focus#focus focus

If I want to look for the substring focus, I need this result (matches in bold):

focus;focus#focus#focus focus

This is as far as I got: (?<!#)(focus)(?!#), demo here.
This pattern obviously does not work because is excluding the substrings next to a #, I need those to be included as well.

Programmatically speaking, this is what I need:

if !(prefix == "#" && suffix == "#") {
    // Is a match
}
1 Answers

You may use an alternation:

(?<!#)focus|focus(?!#)

Updated RegEx Demo

Which means match focus if not preceded by # or not followed by #. That will skip focus only if it has # on both sides.

Related