Updated
I need help with a regex that will find the "@[..[...]]"
pattern.
I will try to explain.
A text will contain placeholders which will be replaced with values upon display of that very same text.
A place holder has 3 parts;
- an open tag, starts with "@[" followed by "a dot delimited text" and ends with "[",
- a property list, a "comma separated list" with qouted (double qoutes) values,
- a close tag, "]]".
The property list items can contain one or many placeholders (nested) and both double qoutes (escaped) and brackets.
The regex must overcome the issues with nested placeholders by knowing when it reached the end of the "outer" placeholder as well as any escaped qoutes and brackets.
Sample
Consider the following text fragment:
Linklist
@[Link.AppText["[startpage]", "startpage"]]
@[Link.Text["[startpage] loggedin", "The \"@[Text.AppText["startpage"]]\" for users"]]
@[Link.Text["@[Link["startpage"]]", "@[Text.AppText["startpage"]]"]]
The match should look like this:
match 1 = @[Link.AppText["[startpage]", "startpage"]]
Gr.1 = Link.AppText
Gr.2 = "[startpage]", "startpage"
match 2 = @[Link.Text["[startpage] loggedin", "The \"@[Text.AppText["startpage"]]\" for users"]]
Gr.1 = Link.Text
Gr.2 = "[startpage] loggedin", "The \"@[Text.AppText["startpage"]]\" for users"
match 3 = @[Link.Text["@[Link["startpage"]]", "@[Text.AppText["startpage"]]"]]
Gr.1 = Link.Text
Gr.2 = "@[Link["startpage"]]", "@[Text.AppText["startpage"]]"
With a solution by @ridgerunner I solved it:
@\[([._\w]+)\[([^[\]""]*(?:""[^""\\]*(?:\\.[^""\\]*)*""[^[\]""]*)*)\]\]
@\[ # Outer open delimiter.
([._\w]+) # 1:st group.
\[ # Inner open delimiter.
( # Start of 2:nd group.
[^[\]""]* # Contents.
(?:""[^""\\]*(?:\\.[^""\\]*)*"" # Contents.
[^[\]""]*)* # Contents.
) # End of 2:nd group.
\]\] # Close delimiter.
And ... for anyone who looks for a "balanced group solution"
... after struggling with google search and a lot of regex testing, I finally figured out another working solution, though I had to alter the pattern slightly to make it work: (at least for me :))
Regex: @([._\w]+)\[\[(""(?:[^\[\]]*|\[[^\[]|[^\]]\]|(?<counter>\[\[)|(?<-counter>\]\]))+(?(counter)(?!))"")\]\] @([._\w]+)\[\[ # start tag, 1:st group ("" # start 2:nd group (?: # non capturing group [^\[\]]* # any char but [ or ] | # or \[[^\[] # if [, not followed by a [ | # or [^\]]\] # if ], not followed by a ] | # or (?<counter>\[\[) # counter start tag | # or (?<-counter>\]\]) # counter stop tag )+ # end non capturing group (?(counter)(?!)) # if counter <> 0, regex fails "") # end 2:nd group \]\] # end tag
Updated placeholders with new pattern; (@..[[...]]
Linklist @Link.AppText[["[startpage]", "startpage"]] @Link.Text[["[startpage] loggedin", "The \"@Text.AppText[["startpage"]]\" for users"]] @Link.Text[["@Link[["startpage"]]", "@Text.AppText[["startpage"]]"]]