views:

34

answers:

1

I have a string that can range from the empty string to an arbitrary list of comma delimited numbers. For example: "1,2,3"

Unfortunately as I write the code to remove an element I have a bunch of if statements--mainly to deal if it is the first, last, or only element in the list. I keep thinking there has got to be a better way!

For example, I would need to be able to remove the element '2' in the following lists:

"1,2,3"
"1,3,2"
"2,1,3"
"2"
"12,2,21"
""
+2  A: 

This should do what you want:

/(\b|,)2(\b|,)/
David Wolever
Ahhh I forgot about \b. That is exactly what I was looking for, thank you!
Zugwalt