Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Regular Expression replace

Posted on 2002-04-08
8
Medium Priority
?
230 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
Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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

596 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