RegExp help
am 08.11.2007 20:41:50 von otrWalter
Note: $code is a single line of code that the previous segment of this
method has located.
I have this...
preg_match('/\bnew wBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches);
$results = $arrMatches[1];
it will find this...
new wBug ( $myvar ); // <-- this is what $code contains
and it will give me...
$myvar
Which is exactly what I want, for this instance, but if I have this...
new wBug ( $myvar, true ); // <-- this is what $code contains,
this time
I get this...
$myvar, true
I'd like to only get....
$myvar
Actually, I'd like to get each parameter in its own array element of
'$arrMatches'
My RegExp is limited on this one.
Can someone throw me a bone?
Thx
walter
Re: RegExp help
am 08.11.2007 21:23:55 von Steve
wrote in message
news:1194550910.202651.104320@t8g2000prg.googlegroups.com...
> Note: $code is a single line of code that the previous segment of this
> method has located.
>
> I have this...
>
> preg_match('/\bnew wBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches);
> $results = $arrMatches[1];
>
>
> it will find this...
>
> new wBug ( $myvar ); // <-- this is what $code contains
>
> and it will give me...
>
> $myvar
wait, wait, wait. so you are opening what apparently is a php script and
parsing it?
try:
/new\s+wBug\s?[^(]*\(\s*([^,)]*)[,)]/i
Re: RegExp help
am 08.11.2007 21:33:46 von luiheidsgoeroe
On Thu, 08 Nov 2007 20:41:50 +0100, otrWalter@gmail.com =
wrote:
> Note: $code is a single line of code that the previous segment of this=
> method has located.
>
> I have this...
>
> preg_match('/\bnew wBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches);
> $results =3D $arrMatches[1];
>
>
> it will find this...
>
> new wBug ( $myvar ); // <-- this is what $code contains
>
> and it will give me...
>
> $myvar
>
> Which is exactly what I want, for this instance, but if I have this...=
>
> new wBug ( $myvar, true ); // <-- this is what $code contains,
> this time
>
> I get this...
>
> $myvar, true
>
> I'd like to only get....
>
> $myvar
>
> Actually, I'd like to get each parameter in its own array element of
> '$arrMatches'
>
> My RegExp is limited on this one.
First of all, are you sure regex is the way to go here? It looks awfully=
=
like a parser should be involved...
And while it's perfectly doable to create a regex for it, why not apply =
=
KISS and:
if(preg_match('/\bnew wBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches)){
$results =3D $explode(',',arrMatches);
array_walk($results,'trim');
}
It's a LOT easier to maintain then a regex...
-- =
Rik Wasmus
Re: RegExp help
am 09.11.2007 05:31:18 von otrWalter
> It's a LOT easier to maintain then a regex...
> --
> Rik Wasmus
Thanks Rik!
Walter
Re: RegExp help
am 10.11.2007 18:30:21 von mik3l3374
On Nov 9, 3:41 am, "otrWal...@gmail.com" wrote:
> Note: $code is a single line of code that the previous segment of this
> method has located.
>
> I have this...
>
> preg_match('/\bnew wBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches);
> $results = $arrMatches[1];
>
> it will find this...
>
> new wBug ( $myvar ); // <-- this is what $code contains
>
> and it will give me...
>
> $myvar
>
> Which is exactly what I want, for this instance, but if I have this...
>
> new wBug ( $myvar, true ); // <-- this is what $code contains,
> this time
>
> I get this...
>
> $myvar, true
>
> I'd like to only get....
>
> $myvar
>
> Actually, I'd like to get each parameter in its own array element of
> '$arrMatches'
>
> My RegExp is limited on this one.
>
> Can someone throw me a bone?
>
> Thx
>
> walter
how about this, no regexp, just simple sub strings
$string = 'new wBug ( $myvar , true )';
$firstpos = strpos($string,"(");
$lastpos = strrpos($string,")");
$str=substr($string,$firstpos+1,$lastpos-$firstpos-1);
if ( strstr($str,",") )
{
$var=explode(",",$str);
echo $var[0];
}
else
{
echo "$str\n";
}
Re: RegExp help
am 12.11.2007 03:02:48 von Steve
wrote in message
news:1194715821.462826.259940@k35g2000prh.googlegroups.com.. .
> On Nov 9, 3:41 am, "otrWal...@gmail.com" wrote:
>> Note: $code is a single line of code that the previous segment of this
>> method has located.
>>
>> I have this...
>>
>> preg_match('/\bnew wBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches);
>> $results = $arrMatches[1];
>>
>> it will find this...
>>
>> new wBug ( $myvar ); // <-- this is what $code contains
>>
>> and it will give me...
>>
>> $myvar
>>
>> Which is exactly what I want, for this instance, but if I have this...
>>
>> new wBug ( $myvar, true ); // <-- this is what $code contains,
>> this time
>>
>> I get this...
>>
>> $myvar, true
>>
>> I'd like to only get....
>>
>> $myvar
>>
>> Actually, I'd like to get each parameter in its own array element of
>> '$arrMatches'
>>
>> My RegExp is limited on this one.
>>
>> Can someone throw me a bone?
>>
>> Thx
>>
>> walter
>
> how about this, no regexp, just simple sub strings
>
> $string = 'new wBug ( $myvar , true )';
> $firstpos = strpos($string,"(");
> $lastpos = strrpos($string,")");
> $str=substr($string,$firstpos+1,$lastpos-$firstpos-1);
> if ( strstr($str,",") )
> {
> $var=explode(",",$str);
> echo $var[0];
> }
> else
> {
> echo "$str\n";
> }
usually a great way to go. however when parsing, there's no good way to get
around the literal difference v. abstract differences between 'new wBug (
$myVar' or 'new wbug($myVar' or any other variation. all are the same
when seen by php, but not by strpos - which looks for literal string
occurances. regex can more easily account for any variation of 'new wBug'
and return exactly what the op is looking for.