tags:

views:

66

answers:

3

I need regex that will match this

Fred, Jhon, Tree,

and there could be some spaces between the commas and the words, and between the start of the line and the first word. Can anyone help me with this ?

A: 
/[\s]*Fred[\s]*,[\s]*Jhon[\s]*,[\s]*,Tree[\s]*,
dj_segfault
The formatting changed my answer. All the [\s] blocks should be followed by an asterick, meaning "0 or more".
dj_segfault
@dj, indenting your text with four spaces will cause it to show up as code. See: http://stackoverflow.com/editing-help
Bart Kiers
B.t.w., the shorthand character class `\s` doe not only match spaces, but all *white space characters*, including tabs and new lines. And there really is no need to put a single shorthand character class inside a character class: `[\s]` is exactly the same as `\s`.
Bart Kiers
A: 

(\w\s*,){3} will matach a word, some or no spaces, and a comma exactly 3 times

ennuikiller
Nope. It will match an alphanumeric (or underscore) followed by zero or more white space characters ending with three comma's. You probably meant to group it all, and then repeat: `(\w+\s*,){3}`. Note that `{3,3}` is the same as `{3}`.
Bart Kiers
Or `(\w+,\s*){3}`
Bart Kiers
@Bart K, yes thanks for catching the capturing group!
ennuikiller
+2  A: 
/^ *Fred *, *Jhon *, *Tree *,$/

Simple explanation: / */ is a regex that matches space zero or more times. Fred matches Fred, John John, etc pp use / +/ instead if you wish at least one space

^ matches the start and $ the end.

note: using /\s*/ fragments instead of / */ fragments matches all whitespace - including tabs, newlines, etc

dionadar
ok, I did, for me it worked like this: \ *Fred\ *,\ *Jhon\ *,\ * Tree\ *, (it's .NET)
Omu