Regex for removing an item from a comma-separated string?
Posted
by
ingredient_15939
on Stack Overflow
See other posts from Stack Overflow
or by ingredient_15939
Published on 2013-11-07T02:53:51Z
Indexed on
2013/11/07
3:54 UTC
Read the original article
Hit count: 111
regex
Say I have a string like this: "1,2,3,11"
A simple regex like this will find a number in the string: (?<=^|,)1(?=,|$)
- this will correctly find the "1" (ie. not the first "1" in "11").
However, to remove a number from the string, leaving the string properly formatted with commas in between each number, I need to include one adjacent comma only. For example, matching 1,
, 2,
or ,11
.
So the trick is to match one comma, on either side of the number, but to ignore the comma on the opposite side (if there is one). Can someone help me with this?
© Stack Overflow or respective owner