A question of spaces ...

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
contiw
Forum Newbie
Posts: 6
Joined: Wed Apr 11, 2007 3:05 pm

A question of spaces ...

Post by contiw »

A string contains just one comma.
The comma must be followed by one space.
I need to eliminate any combination of leading and trailing spaces around that comma only, then add the required space after it.

Please advice how to do it wit RegEx.
ThankYou have a nice weekend.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

preg_replace('#\s{0,},\s{0,}#', ', ', $string);
contiw
Forum Newbie
Posts: 6
Joined: Wed Apr 11, 2007 3:05 pm

Post by contiw »

Thank You Astions. Much appreciate and learning.
ContiW
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

"*" is a more common way to express "{0,}"
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I like to be verbose.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

astions wrote:I like to be verbose.
Yeah, that's a good way to teach. You know, assuming they know how the {} quantifier works. If they do, then giving both examples would clear up any misconception they have about the asterisk, considering it's so overused.

Edit: I didn't mean overused in the sense of being used too much. It *is* a necessity. :D
Post Reply