Objectmix
Tags Register Mark Forums Read

RegExp help : PHP

This is a discussion on RegExp help within the PHP forums in Programming Languages category; 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 ...


Object Mix > Programming Languages > PHP > RegExp help

Reply

 

LinkBack Thread Tools
  #1  
Old 11-08-2007, 02:41 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default RegExp help

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

  #2  
Old 11-08-2007, 03:23 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: RegExp help


<otrWalter@gmail.com> 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


  #3  
Old 11-08-2007, 03:33 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: RegExp help

On Thu, 08 Nov 2007 20:41:50 +0100, otrWalter@gmail.com
<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 = $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 = $explode(',',arrMatches);
array_walk($results,'trim');
}
It's a LOT easier to maintain then a regex...
--
Rik Wasmus
  #4  
Old 11-08-2007, 11:31 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: RegExp help

> It's a LOT easier to maintain then a regex...
> --
> Rik Wasmus


Thanks Rik!

Walter

  #5  
Old 11-10-2007, 12:30 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: RegExp help

On Nov 9, 3:41 am, "otrWal...@gmail.com" <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";
}

  #6  
Old 11-11-2007, 09:02 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: RegExp help


<mik3l3374@gmail.com> wrote in message
news:1194715821.462826.259940@k35g2000prh.googlegroups.com...
> On Nov 9, 3:41 am, "otrWal...@gmail.com" <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.


Reply

Thread Tools


Similar Threads

Thread Thread Starter Forum Replies Last Post
Why, oh, why, little regexp? usenet RUBY 14 10-31-2007 01:25 PM
Regexp help usenet RUBY 7 09-19-2007 06:36 AM
Help on RegExp usenet Javascript 0 04-24-2007 05:56 PM
help with regexp usenet awk 2 04-08-2007 04:48 AM
regexp usenet Perl 3 12-17-2006 10:13 PM


All times are GMT -5. The time now is 08:57 AM.

Managed by Infnx Pvt Ltd.