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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.