1

I need a little help with regex to match and replace

<comma|dash|fullstop|questionmark|exclamation mark|space|start-of-string>WORD<comma|dash|fullstop|space|end-of-string>

I need to find a specific WORD (case insensitive) which

is preceded by: comma or dash or fullstop or question mark or exclamation mark or space or start-of-string

and is followed by: comma or dash or fullstop or question mark or exclamation mark or space or end-of-string

test string: MATCH me, yes please,MATCH me but dontMATCHme!MATCH me and of course MATCH, and finally MATCH

I want to REPLACE all matches with another string in PHP, so i possibly need to use preg_replace or something?

NullUserException
  • 83,810
  • 28
  • 209
  • 234
Sharky
  • 6,154
  • 3
  • 39
  • 72

3 Answers3

1

Try this

$input = "MATCH me, yes please,MATCH me but dontMATCHme!MATCH me and of course MATCH, and finally MATCH";

echo($input."<br/>");
$result = preg_replace("/
                      (?:^             # Match the start of the string
                      |(?<=[-,.?! ]))  # OR look if there is one of these chars before
                      match            # The searched word
                      (?=([-,.?! ]|$)) # look that one of those chars or the end of the line is following
                      /imx",           # Case independent, multiline and extended
                      "WORD", $input);
echo($result);
stema
  • 90,351
  • 20
  • 107
  • 135
1

This is not doing exactly what you asked for, but possibly fulfills your actual requirements better (which I'm guessing to be "Replace MATCH with WORD only if MATCH is an entire word, and not part of a different word"):

$input = 'MATCH me, yes please,MATCH me but dontMATCHme!MATCH me and of course MATCH, and finally MATCH'
$result = preg_replace('/\bMATCH\b/i', "WORD", $input)

The \b are word boundary anchors that only match at the start or end of an alphanumeric word.

Result:

WORD me, yes please,WORD me but dontMATCHme!WORD me and of course WORD, and finally WORD
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

Here is an implementation in PHP that will do the task you described. It will replace all words with "WORD".

<?php

$msg = "MATCH me, yes please,MATCH me but dontMATCHme!MATCH me and of course MATCH, and finally MATCH";

echo($msg."<br/><br/>");
$msg = preg_replace("/(\w)+/", "WORD", $msg);
echo($msg."<br/>");

?>
Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
  • Yes, this definition of "word" (letters, numbers, and underscores) is ok with me. How could i replace a specific word using this approach? – Sharky Sep 20 '11 at 00:55
  • I managed to solve this, i came up with this (Warning: i do not know how effective is this and i hadn't test it thoroughly): eregi_replace('([ -\.,\+\?\(\)\$\[\];_=])'.$oldvalue.'([ -\.,\+\?\(\)\$\[\];_=])',"\\1".$newvalue."\\2",$string); If anyone can transform this to preg_replace i would be gratefull... i posted that question here if anyone can help me out: http://stackoverflow.com/questions/7479185/transform-ereg-replace-to-preg-replace (i post this as comment because SO won't let me self-answer for 5 hours) – Sharky Sep 20 '11 at 01:56
  • I updated the example with working a working php webapp. Try it out and see if it does what you need. Also, I don't think your regex will work if the word is at the beginning or end of a line. – Sky Kelsey Sep 20 '11 at 02:07
  • Thank you for your answer Sky Kelsey, this does what you described, but unfortunately i need to replace a specific word, not just all the words! Um-mm...about my regexp, you have right, it won't work if the word is in beginning of string neither will at the end... i'm still trying to solve this... – Sharky Sep 20 '11 at 02:24
  • So maybe modify your question and specify what words you are looking for? – Sky Kelsey Sep 20 '11 at 03:15