Is this maybe the regex that you're looking for? There may be more elegant ways, but this seems alot easier to decipher.
s/(,42\b|\b42,|^42$)//g;
Update: Added the case for a single element of just "42" per
Not_a_Number's and
Perl Mouse's suggestions. Good catch; that's what a quick solution gets me, I didn't test all cases. I should note too, the position of 42 at the end of the list is important, so the regex engine doesn't try and grab that without looking for commas first.
Update: Perl Mouse makes a good point here:
505335 on boundary checking too, so I made another mod to the regex to take that into account as well.
Update: Ok, this is it; no more updates. One catch w/ boundary checking is in the odd case of "41,I like 42 things, 43", which would replace the "42", but shouldn't. I think in the context of this question this is really isn't an issue and \b works fine. To combat that though, I just hit what really was being addressed, "^42$", where its the only element.
Update: lol. I give up; one more.
Perl Mouse pointed out partial number matches, so the boundary checking is back in a modified form. He has this below too. I only mod it here so in case someone takes a quick glance, they can get my best version (with the help of mightier monks than I) instead of my regex version from like 4 iterations ago.
-jbWare
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.