Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for Commas without a space after them

Tags:

regex

I'm currently trying to make an expression that grabs all commas that do not have a space after them, and all colons.

I've tried (,([^\s]))|([:]), which is close, but also seems to grab the character after the comma that doesn't have a space.

I've also tried (,\s+)|([:]), which will grab all colons and all commas with a space after them as well as the space.

What I would like to be selected is enclosed in * * in the following:

Hello, my name is*:* tibsar*,*okay?

Any ideas?

like image 969
Sara Fuerst Avatar asked Sep 20 '25 23:09

Sara Fuerst


1 Answers

I think this regex will do the work.

Regex: (,(?=\S)|:)

Explanation:

  • (?=\S) This positive lookahead checks for character after , which is not a whitespace but does not capture it in match. [^\s] is same as \S.

  • And used alteration | to match : which may appear anywhere.

Regex101 Demo

To match , at end of line(if needed).


Where you went wrong with (,([^\s]))|([:]) and (,\s+)|([:])?

  • (,([^\s])) Does matches a comma followed by non-whitespace but it also captures it in the match.

  • (,\s+) Straightaway matches a comma followed by a whitespace, so it's completely wrong.


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!