RegEx Find and Replace Help Needed

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

RegEx Find and Replace Help Needed

Postby NoSalt » Mon Jan 25, 2010 10:17 am

Hello All

I am looking for a way to find and replace around a pattern. Say I have a list like the following:

Syntax: [ Download ] [ Hide ]
 
word 192.168.9.10 anotherword
word 192.168.8.20 anotherword
word 192.168.7.30 anotherword
word 192.168.6.40 anotherword
word 192.168.5.50 anotherword
word 192.168.4.60 anotherword
word 192.168.3.70 anotherword
word 192.168.2.80 anotherword
word 192.168.1.90 anotherword
 


and I want to find/replace "word" and "anotherword" with "newword" and "newanotherword" like so:

Syntax: [ Download ] [ Hide ]
 
newword 192.168.9.10 newanotherword
newword 192.168.8.20 newanotherword
newword 192.168.7.30 newanotherword
newword 192.168.6.40 newanotherword
newword 192.168.5.50 newanotherword
newword 192.168.4.60 newanotherword
newword 192.168.3.70 newanotherword
newword 192.168.2.80 newanotherword
newword 192.168.1.90 newanotherword
 


I want to find and replace around a variable string with a definite pattern (like an IP address) without changing said pattern. Is there any way to do this?

Thank you for reading and have a great day. :)
NoSalt
Forum Newbie
 
Posts: 2
Joined: Mon Jan 25, 2010 10:10 am

Re: RegEx Find and Replace Help Needed

Postby ridgerunner » Mon Jan 25, 2010 1:13 pm

NoSalt wrote:... I want to find and replace around a variable string with a definite pattern (like an IP address) without changing said pattern. Is there any way to do this? ...


If I understand you correctly, you simply want to replace 'word' with 'newword' and 'anotherword' with 'newanother word' without affecting any other text in the string. Is this correct?

If so, then a simple string search and replace can do a pretty good job (without resorting to a regular expression) like so:
Syntax: [ Download ] [ Hide ]
$text = str_replace('word', 'newword', $text);
$text = str_replace('anotherword', 'newanotherword', $text);

However, this solution has one problem in that it is not smart enough to know that 'word' should only be replaced if it is found as a "whole" word. If you ran this replace twice in a row, it will happily replace 'newword' with 'newnewword'.

Regular expression to the rescue! With a regex, you can specify to match only whole words by placing the word boundary anchor '\b' at the beginning and end of the word to be matched. Thus, here is an improved solution to your problem:

Syntax: [ Download ] [ Hide ]
 
$text = preg_replace('/\bword\b/', 'newword', $text);
$text = str_replace('/\banotherword\b/', 'newanotherword', $text);


Hope this helps! :)
User avatar
ridgerunner
Forum Contributor
 
Posts: 166
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: RegEx Find and Replace Help Needed

Postby NoSalt » Mon Jan 25, 2010 2:22 pm

That is it but "word" and "newword" can be any word. I just used that as an example.
NoSalt
Forum Newbie
 
Posts: 2
Joined: Mon Jan 25, 2010 10:10 am

Re: RegEx Find and Replace Help Needed

Postby klevis miho » Thu Feb 11, 2010 6:43 am

newword 192.168.1.90 newanotherword

'#[a-z]+ #i' this matches the newword: some characters case insensitive(newword) then a space ' '.
'# [a-z\s]+#i' this matches the newandotherword: a space before ' ', some characters or spaces
klevis miho
Forum Contributor
 
Posts: 289
Joined: Wed Oct 29, 2008 2:59 pm
Location: albania


Return to Regex

Who is online

Users browsing this forum: No registered users and 1 guest