kamaci
kamaci

Reputation: 75207

Perl how to stop regex pattern match when another pattern match occurs?

I have a pattern matcher code as like:

#!/usr/bin/perl
use strict;
use warnings;
open(HTML,"<source.html");
my $html = do {local $/; <HTML>};
$html =~ s/\n\ *//g;
while ($html=~m/<OPTION [^>]*>\D*([^<]+)/g){
    print $1;
    print "\n";
}
close(HTML)

I want to do it until the file however I want to stop and break while loop it it sees any character that matches with a pattern that start with:

</S

How can I do that with Perl?

Upvotes: 1

Views: 614

Answers (2)

ikegami
ikegami

Reputation: 386386

( my $to_search = $html ) =~ s{</S.*}{}s;
while ($to_search =~ m{<OPTION [^>]*>\D*([^<]+)}g) {
    print("$1\n");
}

Upvotes: 0

Shalini
Shalini

Reputation: 455

If you want to exit from a loop , you should use the last command:

last if ( $pattern ~= /^<\/S/ );

Upvotes: 2

Related Questions