BallisticPugh
BallisticPugh

Reputation: 382

Regular Expression: Match a string not immediately preceded by or followed by a alphanumeric character

When searching for d

For example

Upvotes: 3

Views: 1425

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

You're probably looking for word boundary anchors:

\bd\b

matches d only if it isn't adjacent to other alphanumerics.

Note that the definition of "alphanumeric" varies between regex engines. Most define them as the character set [A-Za-z0-9_], but some also include non-ASCII letters/digits.

Upvotes: 4

anubhava
anubhava

Reputation: 785156

You can use lookarounds:

(?<![A-Za-z0-9])[Dd](?![A-Za-z0-9])

Which means match d or D which is not preceded or followed by [A-Za-z0-9].

RegEx Demo

Upvotes: 4

Related Questions