is there regex for this?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
devosc
Forum Newbie
Posts: 4
Joined: Thu Jan 12, 2006 4:26 pm

is there regex for this?

Post by devosc »

Hi

Code: Select all

$this->aVar['Template']->get('MyTest') == 'test' ? $Template.get('MyTest') : $Template.get('MyTest')
I would like to match instances where a sequence of chars start with $ but not $this.

Its to go in a preg_replace, but I've been trying to make it work with preg_match_all while trying to figure it out:

Code: Select all

if (preg_match_all('@\$([^\s]*)@S', $aAttr[$sKey], $aMatches)) {
          var_dump($aAttr[$sKey], $aMatches);
        }

Code: Select all

array
  0 => 
    array
      0 => '$this->aVar['Template']->get('MyTest')'
      1 => '$Template.get('MyTest')'
      2 => '$Template.get('MyTest')'
  1 => 
    array
      0 => 'this->aVar['Template']->get('MyTest')'
      1 => 'Template.get('MyTest')'
      2 => 'Template.get('MyTest')'
I just seem to, or rather, I don't currently know, how to get it to not match $this yet match the others
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

[feyd@home]> php -r "$a = '$this->aVar[\'Template\']->get(\'MyTest\') == \'test\' ? $Template.get(\'MyTest\') : $Template.get(\'MyTest\')'; preg_match_all('#\$(?!this)[A-z_][^\s]*#i',$a,$m); var_export($m);"
array (
  0 =>
  array (
    0 => '$Template.get(\'MyTest\')',
    1 => '$Template.get(\'MyTest\')',
  ),
)
devosc
Forum Newbie
Posts: 4
Joined: Thu Jan 12, 2006 4:26 pm

Post by devosc »

Hi feyd,

Thanks alot, just had to tweak it a little to drop the $; works like a dream. Many Thanks.

G.
Post Reply