Wes Doyle
Wes Doyle

Reputation: 2287

Regex match commas and periods unless surrounded by digits

I have the regular expression:

[^\w-,.]|((?<!\d)[,.]

which matches all non [\w-,.] or, using a negative lookbehind, any comma or period not preceded by a digit.

Using java-compatible regex, I need to capture:

My regex gets me close, however, I am not able to capture the commas and periods in the following examples:

The following example illustrates example of working and non-working strings to match.

https://regex101.com/r/pWcwXa/2

Upvotes: 2

Views: 1099

Answers (1)

Thm Lee
Thm Lee

Reputation: 1236

How about this ?

[^\w-,.]|((?<!\d)[,.]|[,.](?!\d))

Demo,,, in which I slightly changed your regex only to add the case which not followed by a digit

Upvotes: 2

Related Questions