Bernd
Bernd

Reputation: 11493

Regex to match a specific string but only if it doesn't begin with a hashtag

How can I match a specific standalone term but only if it doesn't begin with special characters, specifically “#” or "."

Here is my current regular expression to match standalone terms

(?i)\b(two three)\b

Problem: Checking for boundaries does not seem to consider special characters.

Examples

How can ensure that my pattern is not matching if it is prefaced with characters like “#” and “.”? I'd rather black list special characters than white list the alphabet/number as this should also work well for non-latin languages.

Upvotes: 1

Views: 405

Answers (2)

Peter Cordes
Peter Cordes

Reputation: 364288

So you want to match a given string at the beginning of a word? No need to get fancy, just match beginning-of-a-line | whitespace, then your capturing parens.

`(^|[[:space:]])(two three)\b`

Upvotes: 1

anubhava
anubhava

Reputation: 785186

Use negative lookbehind to fail the match if search term is preceded by # or .:

(?i)(?<![#.])\b(two three)\b

RegEx Demo

Upvotes: 2

Related Questions