Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8301701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:06:40+00:00 2026-06-08T17:06:40+00:00

I wrote a small Perl script with regular expressions to get HTML components of

  • 0

I wrote a small Perl script with regular expressions to get HTML components of a website.

I know its not a good way of doing this kind of job, but I was trying to test out my regex skills.

When run with either one of the two regex patterns in the while loop it runs perfectly and displays the correct output. But when I try to check both patterns in the while loop the second pattern matches every time and the loop runs infinitely.

My script:

#!/usr/bin/perl -w
use strict;

while (<STDIN>) {

    while ( (m/<span class=\"itempp\">([^<]+)+?<\/span>/g) ||
            (m/<font size=\"-1\">([^<]+)+?<\/font>/g) ) {
        print "$1\n";
    }
}

I am testing the above script with a sample input:

<a href="http://linkTest">Link title</a>
<span class="itempp">$150</span>
<font size="-1"> (Location)</font>

Desired output:

$150
(Location)

Thank you! Any help would be highly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:06:42+00:00Added an answer on June 8, 2026 at 5:06 pm

    Whenever a global regex fails to match it resets the position where the next global regex will start searching. So when the first of your two patterns fails it forces the second to look from the beginning of the string again.

    This behaviour can be disabled by adding the /c modifier, which leaves the position unchanged if a regex fails to match.

    In addition, you can improve your patterns by removing the escape characters (" doesn’t need escaping and / needn’t be escaped if you choose a different delimiter) and the superfluous +? after the captures.

    Also use warnings is much better than -w on the command line.

    Here is a working version of your code.

    use strict;
    use warnings;
    
    while (<STDIN>) {
    
        while( m|<span class="itempp">([^<]+)</span>|gc
                or m|<font size="-1">([^<]+)</font>|gc ) {
            print "$1\n";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a small script to let a label move out of the way
I'm writing a small web application using Perl, HTML::Mason and Apache. I've been using
I am writing small process monitor script in Perl by reading values from Proc
I'm teaching myself Perl and Regex by reading Jeffrey Friedl's excellent Mastering Regular Expressions
I am very newbie to Perl. I wrote a very simple Perl program (script):
I'm totally new to Perl, and I need to get a small find and
I wrote a small script to extract data from Oracle using Takusen: {-# LANGUAGE
I wrote a small html/js as shown below: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
I wrote a small express.js based web app. I serve a manifest.webapp but express.js
I wrote two small applications (a client and a server) to test UDP communication

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.