balancing-groups

Regular expression that uses balancing groups

馋奶兔 提交于 2019-12-30 07:43:26
问题 I have a basic text template engine that uses a syntax like this: foo bar %IF MY_VAR some text %IF OTHER_VAR some other text %ENDIF %ENDIF bar foo I have an issue with the regular expression that I am using to parse it whereby it is not taking into account the nested IF/ENDIF blocks. The current regex I'm using is: %IF (?<Name>[\w_]+)(?<Contents>.*?)%ENDIF I have been reading up on balancing capture groups (a feature of .NET's regex library) as I understand this is the recommended way of

Converting PCRE recursive regex pattern to .NET balancing groups definition

一笑奈何 提交于 2019-12-28 05:14:10
问题 PCRE has a feature called recursive pattern, which can be used to match nested subgroups. For example, consider the "grammar" Q -> \w | '[' A ';' Q* ','? Q* ']' | '<' A '>' A -> (Q | ',')* // to match ^A$. It can be done in PCRE with the pattern ^((?:,|(\w|\[(?1);(?2)*,?(?2)*\]|<(?1)>))*)$ (Example test case: http://www.ideone.com/L4lHE) Should match: abcdefg abc,def,ghi abc,,,def ,,,,,, [abc;] [a,bc;] sss[abc;d] as[abc;d,e] [abc;d,e][fgh;j,k] <abc> [<a>b;<c,d>,<e,f>] <a,b,c> <a,bb,c> <,,,> <

How to make balancing group capturing?

痞子三分冷 提交于 2019-12-10 05:19:58
问题 Let's say I have this text input. tes{}tR{R{abc}aD{mnoR{xyz}}} I want to extract the ff output: R{abc} R{xyz} D{mnoR{xyz}} R{R{abc}aD{mnoR{xyz}}} Currently, I can only extract what's inside the {}groups using balanced group approach as found in msdn. Here's the pattern: ^[^{}]*(((?'Open'{)[^{}]*)+((?'Target-Open'})[^{}]*)+)*(?(Open)(?!))$ Does anyone know how to include the R{} and D{} in the output? 回答1: I think that a different approach is required here. Once you match the first larger

How to make balancing group capturing?

别说谁变了你拦得住时间么 提交于 2019-12-05 10:14:39
Let's say I have this text input. tes{}tR{R{abc}aD{mnoR{xyz}}} I want to extract the ff output: R{abc} R{xyz} D{mnoR{xyz}} R{R{abc}aD{mnoR{xyz}}} Currently, I can only extract what's inside the {}groups using balanced group approach as found in msdn . Here's the pattern: ^[^{}]*(((?'Open'{)[^{}]*)+((?'Target-Open'})[^{}]*)+)*(?(Open)(?!))$ Does anyone know how to include the R{} and D{} in the output? I think that a different approach is required here. Once you match the first larger group R{R{abc}aD{mnoR{xyz}}} (see my comment about the possible typo), you won't be able to get the subgroups

Backtracking a balancing group in a greedy repetition may cause imbalance?

a 夏天 提交于 2019-12-01 06:26:41
As a generically brewed example for the purpose of this question, my intent is to match some number of a 's, then an equal number of b 's, plus one more b . Examine the two patterns exhibited in this snippet ( also on ideone.com ): var r1 = new Regex(@"(?xn) (?<A> a)+ (?<B-A> b)+ (?(A)(?!)) b "); var r2 = new Regex(@"(?xn) (?<A> a)+ (?<B-A> b)+? (?(A)(?!)) b "); Console.WriteLine(r1.Match("aaabbb")); // aaabbb Console.WriteLine(r2.Match("aaabbb")); // aabbb Note that there is a difference in the matches of the two patterns. r1 , which uses a greedy repetition on the balancing group construct,

Backtracking a balancing group in a greedy repetition may cause imbalance?

限于喜欢 提交于 2019-12-01 04:54:05
问题 As a generically brewed example for the purpose of this question, my intent is to match some number of a 's, then an equal number of b 's, plus one more b . Examine the two patterns exhibited in this snippet (also on ideone.com): var r1 = new Regex(@"(?xn) (?<A> a)+ (?<B-A> b)+ (?(A)(?!)) b "); var r2 = new Regex(@"(?xn) (?<A> a)+ (?<B-A> b)+? (?(A)(?!)) b "); Console.WriteLine(r1.Match("aaabbb")); // aaabbb Console.WriteLine(r2.Match("aaabbb")); // aabbb Note that there is a difference in

Regular expression that uses balancing groups

拥有回忆 提交于 2019-12-01 01:09:45
I have a basic text template engine that uses a syntax like this: foo bar %IF MY_VAR some text %IF OTHER_VAR some other text %ENDIF %ENDIF bar foo I have an issue with the regular expression that I am using to parse it whereby it is not taking into account the nested IF/ENDIF blocks. The current regex I'm using is: %IF (?<Name>[\w_]+)(?<Contents>.*?)%ENDIF I have been reading up on balancing capture groups (a feature of .NET's regex library) as I understand this is the recommended way of supporting "recursive" regex's in .NET. I've been playing with balancing groups and have so far came up

What are regular expression Balancing Groups?

江枫思渺然 提交于 2019-11-25 22:59:42
问题 I was just reading a question about how to get data inside double curly braces (this question), and then someone brought up balancing groups. I\'m still not quite sure what they are and how to use them. I read through Balancing Group Definition, but the explanation is hard to follow, and I\'m still quite confused on the questions that I mentioned. Could someone simply explain what balancing groups are and how they are useful? 回答1: As far as I know, balancing groups are unique to .NET's regex