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 8017459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:50:59+00:00 2026-06-04T20:50:59+00:00

There is a string(just for testing) and I want to replace all instances of

  • 0

There is a string(just for testing) and I want to replace all instances of <p> under the div, <div id="text">. How do I do that ?

I tested with m and s modifiers, but in vain (Only the first one gets replaced). I have given my Perl code below :

#!/usr/bin/perl
use strict;
use warnings;

my $string = <<STRING;
<div id="main">
    hellohello
    <div id="text">
        nokay.
        <p>This is p1, SHUD B replaced</p>
        Alright
        <p>This is p2, SHUD B replaced</p>
        Yes 2
        <p>this is P3, SHUD B replaced</p>
        Okay done
        bye
    </div>
    bye
    <p>this is not under the div whose id is text and SHUDN'T b replaced</p>
</div>

STRING

my $str_bak = $string;
print "Sring is : \n$string\n\n";

$string =~ s/(<div id="text">.*?)<p>(.*)(<\/p>.*?<\/div>)/$1<p style="text-align:left;">$2 $3/sig;

print "Sring now is : \n$string\n\n";
  • 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-04T20:51:02+00:00Added an answer on June 4, 2026 at 8:51 pm

    Thank you all for the help.

    I could find a regex for that. So I did it with a “workaround”. This is how :

    while( $val =~ s/(<div id="article">.*?)<p>/$1<p style="text-align:left;">/sig )
    {  }
    

    So basically that regex is applicable only to the first match and thats why we are making it repeated in an empty while loop( the loop exits when there are no more match to replace).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a string http://www.ccdcdlmcc.sdc.smdc.,ms.cmcsjh?page=2$#$#L$JK#J$LK#J$ I want to replace the page number with a
I am just testing a small program that I want to test. I am
just to throw some code out there string answer = hotel if (answer ==
I need just dictionary or associative array string => int . There is type
Just a blackout, I want the String hello hallo tjena hej tere to an
There is a string, which comes from text field, and has 200 character limit.
I have a read-only property on a class that I am testing. public string
Just started with JavaCC. But I have a strange behaviour with it. I want
Is there a way to specify that you want an NUnit test to fail,
I know there are already some threads about this, but I just won't work

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.