Custom Regexes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PnHoPob
Forum Newbie
Posts: 16
Joined: Thu Nov 10, 2005 7:16 pm

Custom Regexes

Post 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?
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

then you're going to have to do an if statement to compare the two..unless I cn't understand your question.....
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Re: Custom Regexes

Post by redmonkey »

Code: Select all

$match = array('@([Hello|Hi]) World and \\1 Neighbor@');
PnHoPob
Forum Newbie
Posts: 16
Joined: Thu Nov 10, 2005 7:16 pm

Post 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?
Last edited by PnHoPob on Tue Nov 22, 2005 6:55 pm, edited 1 time in total.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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:

Code: Select all

Hi World and Neighbor
:)
Last edited by Jenk on Tue Nov 22, 2005 7:01 pm, edited 2 times in total.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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:

Code: Select all

Hi World and Neighbor
:)
No, it produces a parse error :P

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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

read again due to my edit please..

and don't use arrays when you are only using one indice.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

try

Code: Select all

echo "Hi World and Neighbor";
And see if it works :lol: :lol: :lol:
PnHoPob
Forum Newbie
Posts: 16
Joined: Thu Nov 10, 2005 7:16 pm

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
Post Reply