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

  • Home
  • SEARCH
  • 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 168819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:31:14+00:00 2026-05-11T12:31:14+00:00

This is a quickly cooked up script, but I am having some difficulty due

  • 0

This is a quickly cooked up script, but I am having some difficulty due to unfamiliarity with regexes and Perl.

The script is supposed to read in an HTML file. There is a place in the file (by itself) where I have a bunch of <div>s. I want to remove every third of them — they are grouped in fours.

My script below won’t compile, let alone run.

#!/usr/bin/perl use warnings; use strict;   &remove();  sub remove {     my $input = $ARGV[0];     my $output = $ARGV[1];     open INPUT, $input or die 'couldn't open file $input: $!\n';     open OUTPUT, '>$output' or die 'couldn't open file $output: $!\n';      my @file = <INPUT>;     foreach (@file) {         my $int = 0;         if ($_ =~ '<div class='cell'>') {         $int++;         { // this brace was the wrong way         if ($int % 4 == 3) {         $_ =~ '/s\<div class='cell'>\+.*<\/div>/;/g';             }     }     print OUTPUT @file; }

Thanks for all your help. I know it is wrong to parse with a regex, but I just want this one to work.

Postmortem: The problem is almost solved. And I shame those who told me that a regex is not good — I knew that to begin with. But then again, I wanted something fast and had programmed the XSLT that produced it. In this case I didn’t have the source to run it again, otherwise I would program it into the XSLT.

  • 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. 2026-05-11T12:31:14+00:00Added an answer on May 11, 2026 at 12:31 pm

    I agree that HTML can’t really be parsed by regexes, but for quick little hacks on HTML that you know the format of, regexes work great. The trick to doing repetition replacements with a regex is to put the repetition into the regex. If you don’t do that you’ll run into trouble syncing the position of the regex matcher with the input you’re reading.

    Here’s the quick-and-dirty way I’d write the Perl. It removes the third div element even when it is nested within the first two divs. The whole file is read and then I use the ‘g’ global replace modifier to make the regex do the counting. If you haven’t seen the ‘x’ modifier before, all it does is let you add spaces for formatting—the spaces are ignored in the regex.

    remove(@ARGV);  sub remove {   my ($input, $output) = @_;    open(INPUT, '<', $input) or die 'couldn't open file $input: $!\n';   open(OUTPUT, '>', $output) or die 'couldn't open file $output: $!\n';    my $content = join('', <INPUT>);   close(INPUT);    $content =~ s|(.*? <div \s+ class='cell'> .*? <div \s+ class='cell'> .*?)                 <div \s+ class='cell'> .*? </div>                 (.*? <div \s+ class='cell'>)|$1$2|sxg;    print OUTPUT $content;   close OUTPUT; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 171k
  • Answers 171k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This issue is somewhat discussed in the Python3 bug list.… May 12, 2026 at 2:18 pm
  • Editorial Team
    Editorial Team added an answer You can switch off Xdebug-var_dump()-overloading by setting xdebug.overload_var_dump to false.… May 12, 2026 at 2:18 pm
  • Editorial Team
    Editorial Team added an answer Certainly unit testing, whether test-first or not, should be your… May 12, 2026 at 2:18 pm

Related Questions

A single installation of our product stores it's configuration in a set of database
Premise I believe that there is a way to objectively define "Good" and "Bad"
So we learned a bit about the Turing Test in my AI class. This
We're building a text templating engine out of a custom HttpModule that replaces tags

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.