You could do this with split - but you shouldn't.
I would recommend a capturing
regex instead.
Here's an example that satisfies your first match:
my $pattern = qr/
(?: # Match beginning of line or whitespace sep
+arator, no capture
\A
|
\s+
)
( # Capture one of:
"[^"]*" # Matched double quotes
| '[^']*' # Matched single quotes
| {[^}]*} # Matched curly brackets
| \S+? # non whitespace
)
(?= # Match end of line or whitespace separator,
+ positive lookahead,no capture.
\z
|
\s+
)
/x;
my $data = '#VER "" 3 19950101 "Overforing BG till PG" 19950608';
my @values = $data =~ /$pattern/g;
for my $i (0..$#values) {
printf "%2d) %s\n",$i,$values[$i];
}
Output:
0) #VER
1) ""
2) 3
3) 19950101
4) "Overforing BG till PG"
5) 19950608
For complex regular expressions like this I would recommend using the 'x' modifier as in the example above. It makes maintenance easier.
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.