Solved

Regular Expression replace

Posted on 2002-04-08
8
221 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
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
 
LVL 84

Expert Comment

by:ozo
ID: 6926386
s(<sitename>.*?</sitename>)(<sitename>Item</sitename>)ig;
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 100 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now