Brethern,
Given a string with:
* an optional dot.separated number at front
* a repeated set of n As and m Bs (n > 0, m >= 0), comma separated
I want to pull off, in a single list, the number, then each of the A and B fields. I.e.,
1.2.3AAABB,AAB,AA,AAABBB
should give
qw(1.2.3 AAA BB AA B AA AAA BBB)
AABB,AAAB,ABBB
should give
qw(AA BB AAA B A BBB)
I've been doing it with a three step process - pull off the numbers, pull off the A and B's, clean up the nulls
use strict;
sub chip{
local($_)=@_;
if(/^(\d+ (?: \.\d+)* )? (.+) /x){
my($front) = $1;
if(my(@rest) = $2 =~ /(A+) (B*) ,? /gx){
unshift(@rest, $front);
grep {$_} @rest}}}
(I've omitted the extra code that does the error-checking etc.)
I want to do a regular match on the numbers, then a greedy match on the rest. I don't think what I did is particularly clear or clean.
Is there a way to do this in a single, comprehensible regex, without using something like grep to clean up the null strings?
throop
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.