This code seems to do what you're looking for:
#!/usr/bin/perl
use strict;
use warnings;
my @data = ( "dist: 45 km;", "dist: 45\nkm;", "dist:\n45 km;" );
foreach my $chunk( @data ){
my( $distance ) = $chunk =~ /dist:.*?(\d+).*?km;/s;
print STDOUT "$distance\n";
}
Here's a breakdown of the regexp:
/dist: # match 'dist:'
.*? # match anything (including newlines), but only until th
+e next part of the pattern starts to match
(\d+) # match 1 or more digits, and put these into $1
.*? # same as before
km; # match 'km;' (not needed unless you want to match a num
+ber of these in one string)
/s # treat the whole line as a single string
Wonko's code works pretty much the same way. I think part of your problem may be that you appended "/ms" to your pattern. 'm' makes the regexp treat the string as multiple lines, and 's' makes the regexp treat the string as a single line, which is what you want it to do in this case. I don't know how those opposing options interact, but that may have caused some of your difficulty.
One other option is to run
$chunk =~ s/\n/ /; over each chunk. That will take all of the newlines out of each, so that all of the strings you gave as examples will evaluate to the same string. That would make a regexp much easier to write.
--
Love justice; desire mercy.
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.