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
Solved

Regular Expression replace

Posted on 2002-04-08
8
222 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever wondered how to display how many visitors you have online. In this tutorial I will show you an easy but effective way to display the number of online visitors in WhizBase. In this article I assume you have read my previous articles and know …
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.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

791 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