tags:

views:

82

answers:

2

i need a regex that converts commas to spaces. I know this is super simple, but i do not know regex. thanks.

tag1, tag2,  tag3, tag4,tag5 tag6

to

tag1 tag2 tag3 tag4 tag5 tag6

thanks

+3  A: 

find: ",\s*" (without quotes)

replace with: " " (without quotes, just a single space)

or:

s/,\s*/ /g
yuku
thanks a lot mate.
laxj11
+1  A: 

Yuku's got it right. Here's in context:

preg_replace('/,\s*/', ' ', 'tag1, tag2,  tag3, tag4,tag5 tag6');

If some of your tags without commas between them have more than one space, you could use this instead:

preg_replace('/,\s*|\s+/', ' ', 'tag1, tag2,  tag3, tag4,tag5 tag6');
Joel
your second regex there returns this: `" t a g 1 t a g 2 t a g 3 t a g 4 t a g 5 t a g 6 "` since `,?\s*` matches an empty string (0 or 1 comma, 0 or more spaces)
nickf
doh -- you're right. Good catch! I've corrected it.
Joel