Page 1 of 1
Custom Regexes
Posted: Tue Nov 22, 2005 3:15 pm
by PnHoPob
"Custom Regexes". Is that what they're called?
Anyways, I'm trying to check if a "custom regex" (something inside parentheses) is the same as another.
For example
Code: Select all
$text = "Hello World and Hi Neighbor";
$match = array('@([Hello|Hi]) World and ([Hello|Hi]) Neighbor@');
$replace = array('\1 World and Neighbor');
echo preg_replace($match, $replace, $text);
That will echo "
Hello World and Neighbor".
However, I don't want it to echo that, since the text had both "Hello" and "Hi".
How can I echo the preg_replace only if
\1 is the same as
\2?
Posted: Tue Nov 22, 2005 6:16 pm
by Charles256
then you're going to have to do an if statement to compare the two..unless I cn't understand your question.....
Re: Custom Regexes
Posted: Tue Nov 22, 2005 6:22 pm
by redmonkey
Code: Select all
$match = array('@([Hello|Hi]) World and \\1 Neighbor@');
Posted: Tue Nov 22, 2005 6:31 pm
by PnHoPob
That's not working for me
Do \1, \2, \3... work on the match array?
What's the difference between \1 and \\1 ?
Charles256 wrote:then you're going to have to do an if statement to compare the two..unless I cn't understand your question.....
Do you know what that if statement would look like?
-----------------------------------------------------------
Also, here's another problem:
Code: Select all
$string = 'For(X,1,7):End';
$match = array ('@For\([A-Z],[0-9],([0-9])\):End@');
$replace = array ('rand \1');
$number = preg_replace($match, $replace, $string);
echo $number;
That echos "rand 7".
How can I make it echo "rand 14", that is, /1 times 2?
Posted: Tue Nov 22, 2005 6:39 pm
by redmonkey
hmm, I just noticed your incorrect use of character classes, perhaps...
Code: Select all
$match = array('@(Hello|Hi) World and \\1 Neighbor@');
... is more what you are looking for.
Posted: Tue Nov 22, 2005 6:53 pm
by Jenk
Code: Select all
$match = "#(Hello|Hi) World and \\1 Neighbor#";
$replace = "\\1 World and Neighbor";
echo preg_replace($match, $replace, "Hi World and Hi Neighbor\n");
outputs:

Posted: Tue Nov 22, 2005 7:00 pm
by redmonkey
Jenk wrote:Code: Select all
$match = "#(Hello|Hi) World and \\1 Neighbor#");
$replace = "$1 World and Neighbor";
echo preg_replace($match, $replace, "Hi World and Hi Neighbor\n");
outputs:

No, it produces a parse error
however...
Code: Select all
$match = "#(Hello|Hi) World and (Hello|Hi) Neighbor#";
$replace = "$1 World and Neighbor";
echo preg_replace($match, $replace, "Hi World and Hello Neighbor\n");
...also produces the same output which unless I'm reading the question wrong, is not the desired result.
Posted: Tue Nov 22, 2005 7:01 pm
by Jenk
read again due to my edit please..
and don't use arrays when you are only using one indice.
Posted: Wed Nov 23, 2005 2:37 am
by jmut
Posted: Fri Nov 25, 2005 6:18 am
by PnHoPob
Jenk wrote:and don't use arrays when you are only using one indice.
Well, I actually need an array since I have a bunch of things. That was just an example code.
I used a lil bit of everybody's idea to come up with this:
Code: Select all
$match = array('@(Hello|Hi) World and \1 Neighbor@');
$replace = "\1 World and Neighbor";
echo preg_replace($match, $replace, "Hi World and Hi Neighbor\n");
Now, does anyone know the answer to the other question? (Maybe I should have made a new thread...)
PnHoPob wrote:Also, here's another problem:
Code: Select all
$string = 'For(X,1,7):End';
$match = array ('@For\([A-Z],[0-9],([0-9])\):End@');
$replace = array ('rand \1');
$number = preg_replace($match, $replace, $string);
echo $number;
That echos "rand 7".
How can I make it echo "rand 14", that is, /1 times 2?
Posted: Fri Nov 25, 2005 7:05 am
by Chris Corbyn
PnHoPob wrote:Code: Select all
$string = 'For(X,1,7):End';
$match = array ('@For\([A-Z],[0-9],([0-9])\):End@');
$replace = array ('rand \1');
$number = preg_replace($match, $replace, $string);
echo $number;
That echos "rand 7".
How can I make it echo "rand 14", that is, /1 times 2?
Try
preg_replace_callback() -- It'll alow you to pull out the backreference and modify it (i.e. multiply) before you use it. That could help you with a few things by the look of it
