Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Regular expression with multiple lines

by PerlNovice999 (Novice)
on Sep 25, 2013 at 07:18 UTC ( #1055629=perlquestion: print w/replies, xml ) Need Help??
PerlNovice999 has asked for the wisdom of the Perl Monks concerning the following question:

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!

Replies are listed 'Best First'.
Re: Regular expression with multiple lines
by choroba (Bishop) on Sep 25, 2013 at 08:25 UTC
    There must be something else going on. I tested your second code and it works for me:
    #!/usr/bin/perl use warnings; use strict; my $content = 'Fields of activity: Legal basis:'; my $d_fieldofactivity = 'missing'; if ($content =~ /Fields \s of \s activity: (.*) Legal/xs) { $d_fieldofactivity = $1; } print "(( $d_fieldofactivity ))\n";


    (( ))
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Regular expression with multiple lines
by Ratazong (Monsignor) on Sep 25, 2013 at 10:05 UTC

    Maybe your website contains the word Legal more than once? you use the greedy match (.*), which would match everything up to the last occurence of Legal. You might want to try using (.*?) instead.

    HTH, Rata

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1055629]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2018-01-19 22:11 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (223 votes). Check out past polls.