CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
need reqular expression in perl to find and replace

 



bostanian
New User

Aug 20, 2007, 12:51 PM

Post #1 of 4 (2561 views)
need reqular expression in perl to find and replace Can't Post

Hi,



can someone help me to write a regular expression to find and reoplace the floowing.

Find string : c:\program files\bac\jnn\gin

Replace String : c:\progra~1\abcdef\ghik\lmn;



thanks

Srini


KevinR
Veteran


Aug 20, 2007, 8:49 PM

Post #2 of 4 (2556 views)
Re: [bostanian] need reqular expression in perl to find and replace [In reply to] Can't Post

What are you actually trying to search and replace? Are you really just trying to replace 'c:\program files\' with 'c:\progra~1\'?

Read my signature regarding PMs. Thank you.
-------------------------------------------------


bostanian
New User

Aug 21, 2007, 6:14 PM

Post #3 of 4 (2554 views)
Re: [KevinR] need reqular expression in perl to find and replace [In reply to] Can't Post

Hi Kevin,



Thanks for your reply, I am trying to replace the later part of \program files\

capturing find sting in a variable

$find = \bac\jnn\gin ;

$replace = \abcdef\ghik\lmn;

by usuing $infile =~ s/$find/$replace/gi ;


which is not working..

Please let me know where am i doing the mistake ..


Thanks
Srini


KevinR
Veteran


Aug 21, 2007, 8:39 PM

Post #4 of 4 (2549 views)
Re: [bostanian] need reqular expression in perl to find and replace [In reply to] Can't Post

You need to use some escaping in the search pattern. You can do that with the \Q[pattern]\E meta characters:

Code
$infile = 'c:\program files\bac\jnn\gin'; 
$find = '\bac\jnn\gin';
$replace = '\abcdef\ghik\lmn';
$infile =~ s/\Q$find\E/$replace/i;
print $infile;


the backslashes in the search pattern will be interpolated if possible by the regexp. For example, \b is a word boundary instead of a literal \b. \p is also a meta character but I don't remember what it means.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives