Page 1 of 1
Template system regex
Posted: Tue Sep 25, 2007 8:38 am
by someberry
I am making a very simple template system. Consider the following code:
I have an expression to pull out all variables (as denoted by the (base_url) in the example), and an expression to pull out outputting variables (as denoted by the whole part of the code).
The question I have is I don't want the first expression to pull out variables which are confined within brackets, as demonstrated in the code - I hope you get what I mean.
Thanks.
Posted: Tue Sep 25, 2007 9:14 am
by feyd
I'm not sure I understand what you're trying to extract. Can you provide an example of the individual pieces you wish to extract?
Posted: Tue Sep 25, 2007 9:26 am
by someberry
Example code:
Code: Select all
Welcome, (username).
The base url is: (output:(base_url))
The first expression should find (username) and (base_url), the second finding (output:(base_url)).
However, because (base_url) is in an output string, (base_url) should not be found by the first expression.
The regex I currently have for the first expression is (it is very basic):
So in essence, I need to change that regex in order to make sure it is not located within another variable.
Thanks.
Posted: Tue Sep 25, 2007 9:58 am
by feyd
Code: Select all
#(?<!\([a-zA-Z0-9_]+:)\([a-zA-Z0-9_]+\)(?!\))#
I believe, but I can't test it where I am right now.
Posted: Tue Sep 25, 2007 11:34 am
by someberry
Hmm, I get the error:
Code: Select all
Warning: preg_match_all() [function.preg-match-all]: Compilation failed: lookbehind assertion is not fixed length at offset 20
Posted: Wed Sep 26, 2007 12:56 am
by GeertDD
PCRE doesn't support lookbehind constructions that can vary in lenght. I've had to work around this problem as well some times.
It depends on the nature of the text you need to check, but my guess is that just looking for a : before and ) after the (variable) should be enough.
Posted: Wed Sep 26, 2007 9:35 am
by feyd
GeertDD wrote:PCRE doesn't support lookbehind constructions that can vary in lenght. I've had to work around this problem as well some times.
It depends on the nature of the text you need to check, but my guess is that just looking for a : before and ) after the (variable) should be enough.
Yeah, I considered leaving it to ":" .. but went with the tighter, albeit unusable, pattern, just in case.