Solved

Regular Expression replace

Posted on 2002-04-08
8
226 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
This article will show, step by step, how to integrate R code into a R Sweave document
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…
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)

739 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