?
Solved

Regular Expression replace

Posted on 2002-04-08
8
Medium Priority
?
228 Views
Last Modified: 2013-12-25
I need to replace all instances of

<sitename>(Anything can be here)</sitename>

with

<sitename>Item</sitename>

how can I accomplish this using regular expressions?

and how can I find all instances of
<sitename>(Anything in here)</sitename)
0
Comment
Question by:Benny00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 6926305
Benny00,

"..I need to replace all instances of..."

Are all these instances in a file?

If yes, then do you want to read this file and then do a search and replace?

Please provide more details on what exactl you are trying to do.

This will help you get a more accurate answer, faster.

"....how can I accomplish this using regular expressions?..."

By regular expressions i am assuming you are referring to Perl regular expressions.

Pl. confirm if this is the case.

0
 

Author Comment

by:Benny00
ID: 6926314
Yes, Perl Regular Expressions.

Search and Replace.

And something that will return me all instances.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6926321
Benny00,

"..Yes, Perl Regular Expressions..."

Excellent!!

"..Search and Replace...."
So i am assuming that this needs to be done on a file (an XML file if i am not wrong)

"..And something that will return me all instances. ..."

This is slightly confusing to me. If you want to do a search and replace, why do you want all the instances to be returned?

Also, when you say "return me all instances." what exactly are you expecting back?

Like i mentioned before, please provide more details.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 84

Expert Comment

by:ozo
ID: 6926386
s(<sitename>.*?</sitename>)(<sitename>Item</sitename>)ig;
0
 
LVL 1

Expert Comment

by:boolee
ID: 7010705
Suppose u want toread and replace from an HTML file and want to print it, use the following code

open (INPUT  ,"sent.html");
while (<INPUT>)
{
     $content .= $_;
}

$content =~ s/<sitename>.*?<\/sitename>/<sitename>(Anything in here)<\/sitename)/gis;
print $content;

0
 
LVL 1

Expert Comment

by:boolee
ID: 7010728
Suppose u want toread and replace from an HTML file and want to print it, use the following code

open (INPUT  ,"sent.html");
while (<INPUT>)
{
     $content .= $_;
}

$content =~ s/<sitename>.*?<\/sitename>/<sitename>(Anything in here)<\/sitename)/gis;
print $content;

0
 
LVL 1

Expert Comment

by:boolee
ID: 7010744
sorry I did it in reverse in the above answer. Here is the corrected one

To replace


open (INPUT  ,"urfile.html");
while (<INPUT>)
{
     $content .= $_;
}

$content =~ s/<sitename>.*?<\/sitename>/<sitename>(Anything in here)<\/sitename>/gis;
print $content;

-------------------------------------------
 To find occurances use

open (INPUT  ,"urfile.html");

while (<INPUT>)
{
     if ($_ =~ /\<sitename\>.*?\<\/sitename\>/)
     {
          print $_;          
     }
}

close(INPUT);

remember to use proper escape characters in ur 'Item'

0
 
LVL 1

Accepted Solution

by:
boolee earned 400 total points
ID: 7010810
sorry I did it in reverse in the above answer. Here is the corrected one

To replace


open (INPUT  ,"urfile.html");
while (<INPUT>)
{
     $content .= $_;
}

$content =~ s/<sitename>.*?<\/sitename>/<sitename>(Anything in here)<\/sitename>/gis;
print $content;

-------------------------------------------
 To find occurances use

open (INPUT  ,"urfile.html");

while (<INPUT>)
{
     if ($_ =~ /\<sitename\>.*?\<\/sitename\>/)
     {
          print $_;          
     }
}

close(INPUT);

remember to use proper escape characters in ur 'Item'

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question