I read this PHP RegEx page, but either I'm missing something, misreading something, or it doesn't work the way they say. I'm guessing it's one of the first two.

$str = preg_replace("([|]\d*)", "\1;", $str);

Could you please update the post providing,

  1. What you are trying to match
  2. The contents of $str before the function call

Edit: The reason why I thought these questions held merit was that regex's can be quite ambiguous. Say for example you were trying to match a pipe or trying to use alteration. If you had no knowledge that the | was the symbol for alteration there could be cause for confusion.


@mk: That has no bearing on the problem at hand, but I'll answer. I'm trying to match a pipe | followed by numbers. I want to replace that with a pipe | followed by numbers followed by a semicolon. The contents of $str do not matter and can be any arbitrary string.

Thomas Owens
+2  A: 

Your regular expression should follow Perl syntax, meaning it has to start and end with the same character (with some exceptions). Also, the back reference should start with a double slash, to get around PHPs double escaping. This should work (with a quick test):

$str = "asdfasdf |123123 asdf iakds |302 asdf |11";
$str = preg_replace("/([|]\d*)/", "\\1;", $str);
echo $str; // prints "asdfasdf |123123; asdf iakds |302; asdf |11;"
Vegard Larsen
Note that the `$n` backreference syntax is preferred over `\\n`. So says the manual.