Need help with simple regex pattern matching

TerryMathews

Lifer
Oct 9, 1999
11,464
2
0
I need to match a comma and any number of spaces and replace it with one space. The closest I can come up with is [,\s*] but its not interpreting correctly. Where am I going wrong?
 

Onund

Senior member
Jul 19, 2007
287
0
0
if you used [,\s*] literally (including []) as your expression then you defined a class of characters to match, not a sequence. Basically regex will check for anything matching in the set regardless of order. You can define a group, or sequence, by using () instead or nothing at all as esun shows.

So, [,\s*] will match a single comma or single space (not entirely sure what the * will do in this context and I'm too lazy to look it up). (,\s*) will match a comma followed by zero or more spaces greedily.
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
Ohh, yeah, Onund is right. If you write [,\s*], it will match a comma, any whitespace character, and any asterisk, which is not what you want.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: esun
Ohh, yeah, Onund is right. If you write [,\s*], it will match a comma, any whitespace character, and any asterisk, which is not what you want.

😕

Please reread what Onund posted.
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
Originally posted by: tfinch2
Originally posted by: esun
Ohh, yeah, Onund is right. If you write [,\s*], it will match a comma, any whitespace character, and any asterisk, which is not what you want.

😕

Please reread what Onund posted.

I did. Aside from the asterisk part, he was right (and he wasn't technically wrong about that, since he said he didn't know). Now what was your point?