blitz5755
blitz5755

Reputation: 23

How to get a right regex to get output like i want

Hello i am using perl and now i don't know how to get ouput like i want. I want only print all digit beetween DIGIT below is my code i hope somebody here can help me to find a right regex.

Please help me ... here my code

#!/usr/bin/perl
my $string = "<TR><TD COLSPAN=2 VALIGN=TOP>Please enter the random key shown below:<TR><TD>&nbsp;<TD VALIGN=TOP><FONT SIZE=+1><FONT COLOR=WHITE>...</FONT>4<FONT COLOR=WHITE>...</FONT>5<FONT COLOR=WHITE>...</FONT>4<FONT COLOR=WHITE>..</FONT>4<FONT COLOR=WHITE>..</FONT>2<FONT COLOR=WHITE>..</FONT>2</FONT></TR>";

if ($string =~  m,</FONT>(\d)<FONT COLOR=WHITE,i) {
    print "$1\n";  #output 454422
} else {
     print "Wrong Regex! \n";
}

Upvotes: 2

Views: 105

Answers (2)

simbabque
simbabque

Reputation: 54373

I'm assuming your desired output is the comment line #output 454422. To get that, you need to wrap your regex in a while-loop and add the /g modifier. Right now, it's only matching once.

my $string =
"<TR><TD COLSPAN=2 VALIGN=TOP>Please enter the random key shown below:<TR><TD>&nbsp;<TD VALIGN=TOP><FONT SIZE=+1><FONT COLOR=WHITE>...</FONT>4<FONT COLOR=WHITE>...</FONT>5<FONT COLOR=WHITE>...</FONT>4<FONT COLOR=WHITE>..</FONT>4<FONT COLOR=WHITE>..</FONT>2<FONT COLOR=WHITE>..</FONT>2</FONT></TR>";

while ( $string =~ m,</FONT>(\d)<FONT COLOR=WHITE,ig ) {
  if ($1) {
    print "$1\n";
  #output 454422
  } else {

    print "Wrong Regex! \n";
  }
}

Upvotes: 1

Tim
Tim

Reputation: 14164

You're looking for the /g flag for "global match", which matches all occurrences of the pattern, as opposed to just the first one.

while ( $string =~  m,</FONT>(\d)<FONT COLOR=WHITE,ig ) {
    print "$1\n";
} # output 45442

Note that the last 2 won't match your pattern. It would if you changed it to:

m,</FONT>(\d)(?:</FONT|<FONT COLOR=WHITE),ig

Upvotes: 1

Related Questions