This looks like an XML element. You might consider an XML parser too heavy for simply grabbing a couple of dates, but parsing libraries exist because XML is not as simple as people wish it were. Regular expressions, as powerful as they are, become the basis for fragile solutions when employed as simple XML parsers.
One problem is that regular expressions alone often are guided to examine a document as a string of characters, without considering its semantic meaning. XML parsers deal with the semantics, and consequently facilitate more reliable parsing.
Here's an example using XML::Twig:
use strict; use warnings; use XML::Twig; my $xml = q{<timeLimit endTime="2016-12-28T23:59:59" startTime="2016-0 +9-30T00:00:00"></timeLimit>}; my $t = XML::Twig->new( twig_handlers => { timeLimit => sub { my $atts = $_->atts; foreach (keys %$atts) { /^(?:start|end)Time$/ && do {print "$_ => $atts->{$_}\ +n"; next;}; } }, }, ); $t->parse($xml);
The output is:
endTime => 2016-12-28T23:59:59 startTime => 2016-09-30T00:00:00
To get output similar to what your script seemed to be attempting, you might do it this way:
my @time_limits; my $t = XML::Twig->new( twig_handlers => { timeLimit => sub { my $atts = $_->atts; if (exists $atts->{startTime} && exists $atts->{endTime}) +{ push @time_limits, [$atts->{startTime}, $atts->{endTim +e}]; } }, }, ); $t->parse($xml); print "[$_->[0]], [$_->[1]]\n" foreach @time_limits;
This produces the following:
[2016-09-30T00:00:00], [2016-12-28T23:59:59]
Notice how it's now not a double-quote issue at all; it's a matter of deciding on a way to drill down to the specific attributes you are interested in and keep track of their content. By side-stepping the regex parsing altogether, we've also avoided issues such as whitespace, newlines showing up mid-element, embedded quotes, and a number of other problems that eventually break regexp-based approaches to scraping XML.
If this is actually HTML as your title states, then use one of the many capable HTML parsers, also on CPAN.
Dave
In reply to Re: Pattern matching and deriving the data between the "(double quotes) in HTML tag
by davido
in thread Pattern matching and deriving the data between the "(double quotes) in HTML tag
by sp4rperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |