Dear Enlightened Ones!

I am extracting some data from a website and am stuck with the following (rather strange) problem. The website has several entries which are always in the same order (and after stripping the html seperated by two new lines). Sometimes a category has no entries, sometimes the entry spans more than one line. So my general strategy is to extract the text between the two category labels using the .* to match any text and using \s to match text if it spans several lines. My problem is that the matching with the next category label, which should be the end of the extracted text, is not recognized. Instead, the rest of the file is returned.

Here is the relevant part of the text from which I am extracting (the text continues after this section). In this case, there is no entry for fields of activity.

Fields of activity: Legal basis:

Thus, the regular expression should either not be triggered at all (there is no text) or it should just include white space or new lines. Instead, the following code returns the rest of the file (including the string "Legal" which should demarcate the end of the extracted bit).

if ($content=~/Fields\sof\sactivity:(.*)\n\nLegal/s) {$d_fieldofactivi +ty=$1;} else {$d_fieldofactivity='missing';}

The same happens with this code (maybe matching the two new lines does not work for some reason???):

if ($content=~/Fields\sof\sactivity:(.*)Legal/s) {$d_fieldofactivity=$ +1;} else {$d_fieldofactivity='missing';}

And with this code (maybe the problem is with words right after new line???):

if ($content=~/Fields\sof\sactivity:(.*)basis/s) {$d_fieldofactivity=$ +1;} else {$d_fieldofactivity='missing';}

Helpful suggestions would be appreciated!


In reply to Regular expression with multiple lines by PerlNovice999

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.