0

I am trying to create a regex to replace a string. Here is my pattern:

\{mailMerge: details_activity_number\\}

When I do a search like this on a string like this:

hello {mailMerge: details_activity_number\}world

its fine. But, if there's a line break like this:

hello \{
mailMerge: details_activity_number\} world

it breaks. Here is my code in PHP:

 $pattern = '\{mailMerge: 'mailMerge: details_activity_number'\}';
 $content =  str_replace($pattern, $value, $content);

Can anyone help me with creating a pattern that would take into consideration possible line breaks/white spaces/etc to guarantee a match?

thanks

EDIT

private function  findAndReplace($content, $mergedArray){
    $test=$content;
    try{
        foreach($mergedArray as $ArrayKey => $ArrayValue){
            foreach ($ArrayValue  as $key => $value) {               
                $pattern = "\{\s*mailMerge:\s+". $key ."\s*\\\}";

                if($value){
                    $test =  preg_replace($pattern, $value, $test);
                }else{
                    $test =  preg_replace($pattern, "No Value Exists", $test);
                }
            }
        }
    }catch(Exception $e){
        throw $e;
    }
    return $test;
}
3
  • Sometimes you have \{ and others you have {. Is the `\` required? Commented Feb 13, 2014 at 19:49
  • Do you want a regular expression or a literal string? str_replace is for replacing literals. There are no regular expression pattern characters in $pattern. Commented Feb 13, 2014 at 19:51
  • Not an answer to your question, but a couple of really good resources for RegEx are regular-expressions.info and Regex Buddy. Commented Feb 13, 2014 at 19:53

1 Answer 1

1

The first version that you posted doesn't allow for any space between { and m. You need to do something like this:

\{\s*mailMerge:\s+details_activity_number\s*\\\}

The \s* means "match zero or more spaces (or other white space, like new lines) here." \s+ means "match one or more spaces (or other white space, like new lines) here."

NOTE: Your code above uses str_replace, but you are trying to do a regex replace. You need to use preg_replace instead of str_replace, like in this code:

$content = "\{mailMerge: 'mailMerge: details_activity_number'\}";
$content =  preg_replace('/\{\s*mailMerge:\s+details_activity_number\s*\\\}/m', $value, $content);

EDIT BASED ON COMMENTS: Try this; it is working for me.

$value = "foo barrrrrr";

$content = "hello
{mailMerge: details_activity_number\} world";

$content =  preg_replace("/\\\\?\\{\s*mailMerge:\s+details_activity_number\s*\\\\?\\}/m", $value, $content);

echo $content; // produces "hello foo barrrrrr world"
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your response. When I run this in a test site like: phpliveregex.com it's fine. But when I run it in my php code it still fails. do I have to escape the last two '\\' to make this work?
Try adding one more \ before the }. \\ just means an escaped \, so there's nothing escaping the }. You should also add /m (for "match across lines"), like this: /\{\s*mailMerge:\s+details_activity_number\s*\\\}/m
Thanks. Still no luck. Again, it works fine in a regex test site but when I run my code it fails (the returned string is empty). I added the full function, if that will help. I really appreciate your help with this, as I'm against a deadline.
I clarified my answer somewhat; the code at bottom should address your problems. Mostly, it has to do with additional escaping.
now I get an error saying " NO MATCHES. CHECK FOR DELIMITER COLLISION.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.