Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Guys help me with Regx

Status
Not open for further replies.

thendal

Programmer
Aug 23, 2000
284
Guys !

How to find a particular text in a file and replace it with another one.

I have a html file in which i have to replace a particular text XXXX with YYYY.


Html file:
<html>
<head>
<title>test</title>
</head>
<body>
XXXX
</body>
</html>


Whether i need to read whole file and when that particular text apperars i have to replace with the new one and then rewrite the whole file with the replaced one.

Is there any shorter way other than this.

Regx Gurus help me.

;-)

[sig][/sig]
 
Good morning thendal,

I don't know of an easy way to avoid reading the entire file and going from there. Unless the file is LARGE, I don't see a problem with reading the entire thing. That said (written),.......

open(IPF,&quot;<SomeFile&quot;) or die &quot;Failed to open SomeFile for reading, $!\n&quot;;
while (<IPF>) { $buffer .= $_; }
close IPF;

# now $buffer contains the entire file.
# so, for the regex part of the question.....

$buffer =~ s/XXXX/YYYY/gis;
# where =~ triggers the pattern match
# [red]s/XXXX/YYYY/[/red] says replace 'XXXX' with 'YYYY'
# and [red]gis[/red] says do it [red]G[/red]lobally,
# [red]I[/red]gnore case,
# and treat the entire thing as one [red]S[/red]tring.

open(OPF,&quot;>SomeFile&quot;) or die &quot;Failed to open SomeFIle to write, $!\n&quot;;
print OPF &quot;$buffer&quot;;
close OPF;


'hope this helps....scream if you need a little more specific help on the regex stuff. [sig]<p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo[/sig]
 
Thanks Goboating.If i find any easy way in Regx i will post it.
:-9

[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top