Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
someberry
Forum Contributor
Posts: 172 Joined: Mon Apr 11, 2005 5:16 am
Post
by someberry » Tue Sep 25, 2007 8:38 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 25, 2007 9:14 am
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?
someberry
Forum Contributor
Posts: 172 Joined: Mon Apr 11, 2005 5:16 am
Post
by someberry » Tue Sep 25, 2007 9:26 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 25, 2007 9:58 am
Code: Select all
#(?<!\([a-zA-Z0-9_]+:)\([a-zA-Z0-9_]+\)(?!\))#I believe, but I can't test it where I am right now.
someberry
Forum Contributor
Posts: 172 Joined: Mon Apr 11, 2005 5:16 am
Post
by someberry » Tue Sep 25, 2007 11:34 am
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
GeertDD
Forum Contributor
Posts: 274 Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium
Post
by GeertDD » Wed Sep 26, 2007 12:56 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Sep 26, 2007 9:35 am
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.