Closed Thread Icon

Topic awaiting preservation: Regular Expression Syntax (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=13016" title="Pages that link to Topic awaiting preservation: Regular Expression Syntax (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Regular Expression Syntax <span class="small">(Page 1 of 1)</span>\

 
cpriest
Obsessive-Compulsive (I) Inmate

From: San Francisco, CA, USA
Insane since: Dec 2003

posted posted 12-15-2003 00:32

Hello All, I'm fairly young in my understanding of regular expression syntax and need some help.

If I have this text:

This is a test @admin_only(<a href='abcd?123=@field(Yabba);'>This is the link</a> ); of the emergency broadcast system.

I'm using PHP and I want to be able to scan this and pull out something along these lines:
1st Match: @admin_only(...);
2nd Match: @field(Yabba);

What I've been using to match against this before has been /@(\w+)\(([^)]+)\);/

The problem of course with that is that I cannot match nested parenthesis, I need for regular expression somehow to count the open parenthesis within and equal them to closed parenthesis before matching the full expression..

Any help would be greatly appreciated!

Thanks

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 12-15-2003 13:53

Impossible.
Regular expression's can't do that.
It has to do with the complexity of the grammar you're using, and regular expressions are a level below what you need.
(Edit: Specifically, the rule of thumb is 'regular expressions can't count').

Now, fear not, for this is easy to do with a simple, straight forward parser
Lo and behold, try to understand this code, and though shall be enligthend (*)

code:
<?php
$strText = "This is a test @admin_only(<a href='abcd?123=@field(Yabba);'>This is the link</a> ); of the emergency broadcast system.";

$iOffset = 0;
$arrResults = array(); //array(array(command,parameter))
while (true)
{
$iOffset = strpos($strText,'@',$iOffset);
if ($iOffset === false)
break;
$strCommand = '';
while (($strText{$iOffset} != '(') && ($iOffset < strlen($strText)))
{
$strCommand .= $strText{$iOffset};
$iOffset++;
}
$strParameter = '';
$iOpenCounter = 1;
$iCloseCounter = 0;
$iInnerOffset= 1; //so we don't count the opening ( twice
while ($iOffset + $iInnerOffset < strlen($strText))
{
if ($strText{$iOffset + $iInnerOffset} == '(')
$iOpenCounter++;
elseif ($strText{$iOffset + $iInnerOffset} == ')')
$iCloseCounter++;
else
$strParameter .= $strText{$iOffset + $iInnerOffset};
if ($iOpenCounter == $iCloseCounter)
break;
$iInnerOffset++;
}
$arrResults[] = array($strCommand,$strParameter);
$iOffset += 1; //we could advance expression length, but then we wouldn't catch any commands within a expression
}
print_r($arrResults);
?>



* no gurantee for enligthenment.

so long,
Tyberius Prime.

[This message has been edited by Tyberius Prime (edited 12-15-2003).]

cpriest
Obsessive-Compulsive (I) Inmate

From: San Francisco, CA, USA
Insane since: Dec 2003

posted posted 12-16-2003 15:41

Thanks for your help Tyberius, was hoping regular expression might be able to do it for me, guess not though. I'll use the code you posted instead, thanks again!



« BackwardsOnwards »

Show Forum Drop Down Menu