Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One comma in space

Tags:

regex

php

Find, for example in

yyyy , yyyy

yyyy,yyyy

yyyy, yyyy

yyyy ,yyyy

the pattern between yyyy and yyyy.

I've tried:

(\s*,{1}\s*)

but this will also match

yyyy,,yyyy.

Demo

What is missing?


1 Answers

Using lookarounds:

(?<![,\s])\s*,\s*(?![,\s])
  • (?<![,\s]) is a negative lookbehind (?<!pattern). In this case, "not preceded by a comma or a whitespace"
  • (?![,\s]) is a negative lookahead (?!pattern). In this case, "not followed by a comma or a whitespace"
like image 159
Mariano Avatar answered Mar 07 '26 17:03

Mariano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!