regex-recursion

Regex Recursion: Nth Subpatterns

邮差的信 提交于 2021-01-27 13:44:07
问题 I'm trying to learn about Recursion in Regular Expressions, and have a basic understanding of the concepts in the PCRE flavour. I want to break a string: Geese (Flock) Dogs (Pack) into: Full Match: Geese (Flock) Dogs (Pack) Group 1: Geese (Flock) Group 2: Geese Group 3: (Flock) Group 4: Dogs (Pack) Group 5: Dogs Group 6: (Pack) I know neither regex quite does this, but I was more curious as to the reason why the the first pattern works, but the second one doesn't. Pattern 1: ((.*?)(\(\w{1,}\)

How exactly does this recursive regex work?

℡╲_俬逩灬. 提交于 2019-12-30 09:29:23
问题 This is a followup to this question. Have a look at this pattern: (o(?1)?o) It matches any sequence of o with a length of 2 n , with n ≥ 1. It works, see regex101.com (word boundaries added for better demonstration). The question is: Why? In the following, the description of a string (match or not) will simply be a bolded number or a bolded term that describes the length, like 2 n . Broken down (with added whitespaces): ( o (?1)? o ) ( ) # Capture group 1 o o # Matches an o each at the start

Capturing text before and after a C-style code block with a Perl regular expression

痞子三分冷 提交于 2019-12-17 16:58:14
问题 I am trying to capture some text before and after a C-style code block using a Perl regular expression. So far this is what I have: use strict; use warnings; my $text = << "END"; int max(int x, int y) { if (x > y) { return x; } else { return y; } } // more stuff to capture END # Regex to match a code block my $code_block = qr/(?&block) (?(DEFINE) (?<block> \{ # Match opening brace (?: # Start non-capturing group [^{}]++ # Match non-brace characters without backtracking | # or (?&block) #

How exactly does this recursive regex work?

穿精又带淫゛_ 提交于 2019-12-01 06:08:39
This is a followup to this question . Have a look at this pattern: (o(?1)?o) It matches any sequence of o with a length of 2 n , with n ≥ 1. It works, see regex101.com (word boundaries added for better demonstration). The question is: Why? In the following, the description of a string (match or not) will simply be a bolded number or a bolded term that describes the length, like 2 n . Broken down (with added whitespaces): ( o (?1)? o ) ( ) # Capture group 1 o o # Matches an o each at the start and the end of the group # -> the pattern matches from the outside to the inside. (?1)? # Again the

Capturing text before and after a C-style code block with a Perl regular expression

与世无争的帅哥 提交于 2019-11-28 01:57:11
I am trying to capture some text before and after a C-style code block using a Perl regular expression. So far this is what I have: use strict; use warnings; my $text = << "END"; int max(int x, int y) { if (x > y) { return x; } else { return y; } } // more stuff to capture END # Regex to match a code block my $code_block = qr/(?&block) (?(DEFINE) (?<block> \{ # Match opening brace (?: # Start non-capturing group [^{}]++ # Match non-brace characters without backtracking | # or (?&block) # Recursively match the last captured group )* # Match 0 or more times \} # Match closing brace ) )/x; # $2