With that additional qualification, it will get a bit more tricky. My first thought was to add a space to the character class: m,"?(/[^" ]*)"?,
But that doesn't work because it won't care that it has found a space inside or outside of a quote, and will stop the regex. Meaning it would capture just "/bootMe" from the line "/bootMe any text here".
I'd suggest looking into a module like Text::xSV or Text::CSV_XS and setting the delimiter to spaces. Then reject any entry that doesn't have a leading slash. This means dropping the regex entirely.
Something like:
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV_XS;
my $csv = Text::CSV_XS->new ({sep_char => ' '});
while (my $line = <DATA>) {
chomp $line;
# See perldoc Text::CSV_XS for warnings
# about this approach with possible embedded
# newlines:
my $status = $csv->parse($line);
my @fields;
if ($status) {
@fields = $csv->fields();
}
else {
warn "Problem parsing $line\n";
}
for my $field (@fields) {
print "Captured ($field) from $line\n" if $field =~ m!^/!;
}
}
__DATA__
"/moreIters 10"
"/bootMe any text here"
/fewIter
/some stuff here
"/albatross" foo bar baz
monkeys
leprechauns /not monkeys
/gnomes "not leprechauns though"
/gnomes more data here
Which gives the output:
Captured (/moreIters 10) from "/moreIters 10"
Captured (/bootMe any text here) from "/bootMe any text here"
Captured (/fewIter) from /fewIter
Captured (/some) from /some stuff here
Captured (/albatross) from "/albatross" foo bar baz
Captured (/not) from leprechauns /not monkeys
Captured (/gnomes) from /gnomes "not leprechauns though"
Captured (/gnomes) from /gnomes more data here
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|