in reply to RegEx to match at least one non-adjacent term
Which gives the following on various runs:use strict; while (<STDIN>) { s/[\s\(\)]+//g; # strip optional chars my @matches = m/(\d+)|([^\d]+)/g; print join ":", grep { defined $_ and $_ !~ m/^(r|rd|red)$/i } + @matches; }
Update: Ikegami pointed out that I missed the requirement that spaces/parens were optional, so I changed this around slightly (no longer just a one liner). Now we can just get the numbers and words out seperately and print them in order skipping any words (like variations of red) that you need.$ echo "12345 (Gray) 6789 (Red)" | ./red.pl 12345:Gray:6789 $ echo "12345 (Gray) 6789 Red" | ./red.pl 12345:Gray:6789 $ echo "12345Gray6789Red" | ./red.pl 12345:Gray:6789
---
s;;:<).>|\;\;_>?\\^0<|=!]=,|{\$/.'>|<?.|/"&?=#!>%\$|#/\$%{};;y;,'} -/:-@[-`{-};,'}`-{/" -;;s;;$_;see;
Warning: Any code posted by tuxz0r is untested, unless otherwise stated, and is used at your own risk.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: RegEx to match at least one non-adjacent term
by ikegami (Patriarch) on Dec 07, 2007 at 16:26 UTC |