PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > RegExp
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
RegExp

Réponse
 
LinkBack Outils de la discussion
Vieux 08/11/2007, 19h41   #1
otrWalter@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RegExp

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

  Réponse avec citation
Vieux 08/11/2007, 20h23   #2
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: RegExp


<otrWalter@gmail.com> wrote in message
news:1194550910.202651.104320@t8g2000prg.googlegro ups.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


  Réponse avec citation
Vieux 08/11/2007, 20h33   #3
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: RegExp

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
  Réponse avec citation
Vieux 09/11/2007, 04h31   #4
otrWalter@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: RegExp

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


Thanks Rik!

Walter

  Réponse avec citation
Vieux 10/11/2007, 17h30   #5
mik3l3374@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: RegExp

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";
}

  Réponse avec citation
Vieux 12/11/2007, 02h02   #6
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: RegExp


<mik3l3374@gmail.com> wrote in message
news:1194715821.462826.259940@k35g2000prh.googlegr oups.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.


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 08h06.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11361 seconds with 14 queries