recursive-regex

Why will this recursive regex only match when a character repeats 2^n - 1 times?

自闭症网瘾萝莉.ら 提交于 2020-01-11 02:32:07
问题 After reading polygenelubricants's series of articles on advanced regular expressions techniques (particularly How does this Java regex detect palindromes?), I decided to attempt to create my own PCRE regex to parse a palindrome, using recursion (in PHP). What I came up with was: ^(([a-z])(?1)\2|[a-z]?)$ My understanding of this expression is that it should either match zero or one characters (every string of less than 2 characters is implicitly a palindrome, as well as to account for

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> <,,,> <

Recursive PCRE search with patterns

房东的猫 提交于 2019-12-25 04:46:07
问题 This question has to do with PCRE . I have seen a recursive search for nested parentheses used with this construct: \(((?>[^()]+)|(?R))*\) The problem with this is that, while the ' [^()]+ ' can match any character including newline, you are forced to match only single-character characters, such as braces, brackets, punctuation, single letters, etc. What I am trying to do is replace the '(' and ')' characters with ANY kind of pattern (keywords such as 'BEGIN' and 'END', for example). I have

Regex for nested XML attributes

早过忘川 提交于 2019-12-20 07:20:12
问题 Lets say I have following string: "<aa v={<dd>sop</dd>} z={ <bb y={ <cc x={st}>ABC</cc> }></bb> }></aa>" How can I write general purpose regex (tag names change, attribute names change) to match content inside {} , either <dd>sop</dd> or <bb y={ <cc x={st}>ABC</cc> }></bb> . Regex I wrote "(\s*\w*=\s*\{)\s*(<.*>)\s*(\})" matches "<dd>sop</dd>} z={ <bb y={ <cc x={st}>ABC</cc> }></bb>" which is not correct. 回答1: In generic regex there's no way to handle nesting in a good way. Hence all the

Recursive pattern in regex

≯℡__Kan透↙ 提交于 2019-12-17 03:00:11
问题 This is very much related to Regular Expression to match outer brackets however, I specifically want to know how or whether it's possible to do this regex's recursive pattern? I'm yet to find a python example using this strategy so think this ought to be a useful question! I've seen some claims that recursive patterns can be used to match balanced parenthesis, but no examples using python's regex package (Note: re does not support recursive pattern, you need to use regex). One claim is that

Recursive pattern in regex

∥☆過路亽.° 提交于 2019-12-17 03:00:01
问题 This is very much related to Regular Expression to match outer brackets however, I specifically want to know how or whether it's possible to do this regex's recursive pattern? I'm yet to find a python example using this strategy so think this ought to be a useful question! I've seen some claims that recursive patterns can be used to match balanced parenthesis, but no examples using python's regex package (Note: re does not support recursive pattern, you need to use regex). One claim is that

Matching Nested Structures With Regular Expressions in Python

痞子三分冷 提交于 2019-12-17 02:38:08
问题 I seem to remember that Regular Expressions in DotNet have a special mechanism that allows for the correct matching of nested structures, like the grouping in " ( (a ( ( c ) b ) ) ( d ) e ) ". What is the python equivalent of this feature? Can this be achieved using regular expressions with some workaround? (Though it seems to be the sort of problem that current implementations of regex aren't designed for) 回答1: You can't do this generally using Python regular expressions. (.NET regular

Matching Nested Structures With Regular Expressions in Python

做~自己de王妃 提交于 2019-12-17 02:38:07
问题 I seem to remember that Regular Expressions in DotNet have a special mechanism that allows for the correct matching of nested structures, like the grouping in " ( (a ( ( c ) b ) ) ( d ) e ) ". What is the python equivalent of this feature? Can this be achieved using regular expressions with some workaround? (Though it seems to be the sort of problem that current implementations of regex aren't designed for) 回答1: You can't do this generally using Python regular expressions. (.NET regular

Java regex: How to replace all character inside a bracket?

本小妞迷上赌 提交于 2019-12-12 04:07:40
问题 How do I able to replace: ((90+1)%(100-4)) + ((90+1)%(100-4/(6-4))) - (var1%(var2%var3(var4-var5))) with XYZ((90+1),(100-4)) + XYZ((90+1),100-4/(6-4)) - XYZ(var1,XYZ(var2,var3(var4-var5))) with regex? Thanks, J 回答1: this doesn't really look like a very good job for a regex. It looks like you might want to write a quick recursive descent parser instead. If I understand you correctly, you want to replace the infix operator % with a function name XYZ? So (expression % expression) becomes XYZ

Recursive regex with garbled text surrounding? Getting “ArrayArray”

≯℡__Kan透↙ 提交于 2019-12-11 23:55:34
问题 I asked a similar question, but it was closed for being too broad. Basically, I have a bunch of questions like this. I'm hoping just asking one will be easier. I've tried some different ways to solve this, but none of them actually work. I have a text file with a lot of data. The only data that I'm interested in falls between two brackets, "(" ")". I'm wondering how to get each instance of info that lies between brackets into an array. The code I'm using right now returns ArrayArray :