Template system regex

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

Moderator: General Moderators

Post Reply
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Template system regex

Post by someberry »

I am making a very simple template system. Consider the following code:

Code: Select all

(output:(base_url))
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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Post 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):

Code: Select all

\([a-z0-9_]\)
So in essence, I need to change that regex in order to make sure it is not located within another variable.

Thanks.
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

#(?<!\([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 »

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
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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.

Code: Select all

#(?<!:)\([a-z0-9_]+\)(?!\))#i
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply