Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot get multi-line regex to match string

I’m reading an HTML file, trying to get some information out of it. I’ve tried HTML parsers, but can’t figure out how to use them to get key text out. The original reads the html file, but this version is a minimal working example for StackOverflow purposes.

#!/usr/bin/env perl

use 5.036;
use warnings FATAL => 'all';
use autodie ':default';
use Devel::Confess 'color';

sub regex_test ( $string, $regex ) {
    if ($string =~ m/$regex/s) {
        say "$string matches $regex";
    } else {
        say "$string doesn't match $regex";
    }
}
# the HTML text is $s
my $s = '      rs577952184 was merged into
      
        <a target="_blank"
           href="rs59222162">rs59222162</a>
      
';

regex_test ( $s, 'rs\d+ was merged into.*\<a target="_blank".+href="rs(\d+)/');

however, this doesn’t match.

I think that the problem is the newline after "merged into" isn’t matching.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I alter the above regex to match $s?

>Solution :

The problem is the trailing / character in the $regex, which should either be omitted or changed to "

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading