Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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!

Comment on Regular expression with multiple lines
Select or Download Code
Re: Regular expression with multiple lines
by choroba (Abbot) 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";

    Output:

    (( ))
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Regular expression with multiple lines
by Ratazong (Prior) 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
Reaped: Re: Regular expression with multiple lines
by NodeReaper (Curate) on Sep 25, 2013 at 10:06 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1055629]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (13)
As of 2014-09-02 12:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (22 votes), past polls