Questions tagged [recursive-regex]

Using a regular expression to parse an expression which contains the same expression e.g. `{ foo bar {zoo} }`

A recursive expression is an expression which contains the same expression, e.g. { foo bar {zoo} } contains an expression {...} which contains an expression {...}. Such expressions are typically expressed using a Context-free language.

However, a regular expression is only capable to parse an expression which uses a Regular Grammar.

Thus, this tag suggests that a user wants to parse a recursive expression using a regular expression, which is not possible. Typically, the user knows regular expressions, but is not really aware of the limitations, so it asks the SO community.

There is no unique solution to that problem since it depends on the grammar of the expression to parse (e.g. the user may want to limit the recursion levels).

Recursive expressions should not be confused with repeated expressions (e.g. {foo} {bar} {zoo}) which can be parsed using regular expressions.

49 questions
1
vote
1 answer

Parsing balanced nested wiki templates and extract a single line parameter's content by a regexp

I know parsing nested strings or HTML is better done by a real parser but in my case I have simple templates and wanted to extract the title content of a Wiki parameter 'title' from a template. It took me a while to achieve this but thanks to the…
1
vote
1 answer

Pro regex converting these impossible-to-regex examples?

Example of input vulture (wing) tabulations: one leg; two legs; flying father; master; patriarch mat (box) pedistal; blockade; pilar animal belly (oval) old style: naval jackal's belly; jester slope of hill (arch) key; visible;…
Wolfpack'08
  • 3,982
  • 11
  • 46
  • 78
1
vote
1 answer

Named groups in recursive pattern Match

I'm matching on LaTeX-Commands of the form \command{...}{...}. The second argument is optional. My RegEx is a only slightly modificated version of one example in perl6 faq because I need to take care of the case that there may be nested LaTeX…
Secoe
  • 634
  • 4
  • 12
1
vote
2 answers

How to write a Ruby-regex pattern in Java (includes recursive named-grouping)?

well... i have a file containing tintin-script. Now i already managed to grab all actions and substitutions from it to show them properly ordered on a website using Ruby, which helps me to keep an overview. Example TINTIN-script #substitution {You…
Ingo
  • 1,805
  • 18
  • 31
1
vote
1 answer

Recursive match with regular expression

I need to split an expression with the following strings: 'with', 'select', 'from', 'where' These will come in the sequence as I wrote. You may assume that each of the words will begin and end with word boundary. I have done this without any…
Mohayemin
  • 3,841
  • 4
  • 25
  • 54
1
vote
0 answers

Ruby strange behavior with recursive regex

I recently stumbled into this trying to use a recursive regex: '--01-0-01'.scan /0|1|-(\g<0>)(\g<0>)/ #=> [['0', '-0-01']] where it seems that the first recursive pattern matched deeper than expected wile the second one performed normally. This is…
Asone Tuhid
  • 539
  • 4
  • 13
1
vote
4 answers

Remove all empty HTML tags?

I am imagining a function which I figure would use Regex, and it would be recursive for instances like

to remove all empty HTML tags within a string. This would have to account for whitespace to if possible. There would be…
SventoryMang
  • 10,275
  • 15
  • 70
  • 113
1
vote
2 answers

Matching Parentheses with Division Operator - Regex

Examples: input: (n!/(1+n)) output: frac{n!}{1+n} input: ((n+11)!/(n-k)^(-1)) output: frac{(n+11)!}{(n-k)^(-1)} input: (9/10) output: frac{9}{10} input: ((n+11)!/(n-k)^(-1))+(11)/(2) output: frac{(n+11)!}{(n-k)^(-1)}+(11)/(2) The following regex…
SamB
  • 2,621
  • 4
  • 34
  • 39
1
vote
1 answer

Ruby regex for matching simpliest Ruby's regexes

I want to match regexes (at least the basic ones, not all their possible kinds... for now...) in a text of Ruby script. It's something like a... \/\^? oh my god... \$?\/[eimnosux]* Maybe I need recursive regex here.
Nakilon
  • 34,866
  • 14
  • 107
  • 142
1
vote
5 answers

What reg expression patten to I need to match everything between {{ and }}

What reg expression patten to I need to match everything between {{ and }} I'm trying to parse wikipedia, but im ending up with orphan }} after running the rexex code. Here's my PHP script.
Stuart
  • 13
  • 1
  • 1
  • 4
1
vote
0 answers

Recursive regex pattern in PHP

I'd like to use tag-style annotations in html text to replace sections of text/html depending on variable names using PHP. The replacement itself works perfectly if not using nested tags. But if there are nested tags, only the outer one gets…
wullxz
  • 17,830
  • 8
  • 32
  • 51
1
vote
1 answer

Recursive pattern in NSRegularExpression

Similar question to Recursive pattern in regex, but in Objective-C. I want to find the ranges or substrings of outer brackets. Example input: NSString *input = @"{a {b c}} {d e}"; Example result: // yeah, I know, we can't put NSRange in an array,…
Cœur
  • 37,241
  • 25
  • 195
  • 267
1
vote
1 answer

Recursive group capturing regex with backreference in JAVA

I am trying to capture multiple groups recursively in a string using also a backreference to a group within the regex. Even though I am using a Pattern and a Matcher and a "while(matcher.find())" loop, it is still only capturing the last instance…
JohnRDOrazio
  • 1,358
  • 2
  • 15
  • 28
1
vote
1 answer

Create function calls to returned functions parser in PyParsing

I want to parse function calls that can call functions 'returned' by other functions: thisReturnsFunction()() I have done this: id = Regex(r'[_a-zA-Z][_a-zA-Z0-9]*') funcal = Forward() value = funcal | id funcal << value +…
R.O.S.S
  • 605
  • 5
  • 18
1
vote
0 answers

PHP/Regex recursive tags in html template

I'm using my own PHP code to render template files as HTML codes. I need a regex to parse the recursive tags correctly, in this example it's the permission tags. Example: Template code: Show this
Tsuki
  • 33
  • 5