I am trying to split() a string, and capture only some of the separators. I thought it would be more straightforward than it turns out to be. Heres one try that works:
my @list = grep defined($_), split /
\s+
|(?<=[A-Z])(?=[0-9]) # letter-number
|(?<=[0-9])(?=[A-Z]) # number-letter
|
(
,
|\+
|-
|\/
|&
)
/xi,
$string;
I think this will always work, because the only ones i want to capture are in the parentheses, and when one of the others matches, the value of $1 is undef. But i was looking to do this with just split(), perhaps its not possible.
Is this a good way to do this, or am i missing something obvious?
UPDATE:
Ack! I already found something that doesnt work right:
129-129A & B-131 NORTH AV
The result is quite strange though, some of the whitespace are captured, others are not. The list, printed one per line is:
token: 129
token: -
token: 129
token: A
token:
token: &
token:
token: B
token: -
token: 131
token: NORTH
token: AV
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.