Page 1 of 1

Regex

Posted: Tue Feb 17, 2009 8:21 am
by Darkzaelus
Hello, i'm at my wits end with this piece of code.
I'm rewriting my template engine to deal with multiple parts of an if statement, eg:

Code: Select all

{if $login_disabled&&$login_disabled}
I have a preg_replace_callback function that works fine, it gets the

Code: Select all

$login_disabled&&$login_disabled
part out of the statement.

Only problem now is that whenever I do a regex statement, match or replace, it only captures the first letter after the dollar sign:

Code: Select all

 
preg_match('/\\$([a-z0-9]+?)/is', $matches[1], $match);
 
yields:

Code: Select all

 
Array
(
    [0] => $l
    [1] => l
)
 
What i'm trying to do is get it into the form of:

Code: Select all

 
<?php if($this->PV_vars['login_disabled']): ?>
 
with this code:

Code: Select all

 
preg_replace('/\\$([a-z0-9]+?)/is', '\$this->PV_vars(\'$1\')', $matches[1]);
 
Instead what comes out is:

Code: Select all

 
<?php if(!$this->PV_vars('l')ogin_disabled): ?>
 
Is there something stupid i'm doing?


Cheers,

Darkzaelus

Re: Regex

Posted: Tue Feb 17, 2009 8:35 am
by mintedjo
Uhmmmm

Code: Select all

[a-z0-9]+?
is a lazy match, meaning it will match the smallest string that matches and fits in with the surrounding regex (if its possible).
In this case the smallest string is the first alphabetic character - "l".

Try replacing

Code: Select all

[a-z0-9]+?
with

Code: Select all

[a-z0-9_]+
?

Re: Regex

Posted: Tue Feb 17, 2009 8:37 am
by Darkzaelus
hah, cheers for that.

I feel a bit of a pillock now lol.

Darkzaelus.

Re: Regex

Posted: Tue Feb 17, 2009 8:38 am
by mintedjo
Darkzaelus wrote:I feel a bit of a pillock now
Because you are...


j/k