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.

48 questions
63
votes
2 answers

Recursive pattern in regex

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…
Andy Hayden
  • 291,328
  • 80
  • 565
  • 500
22
votes
2 answers

Converting PCRE recursive regex pattern to .NET balancing groups definition

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…
kennytm
  • 469,458
  • 94
  • 1,022
  • 977
18
votes
6 answers

Matching Nested Structures With Regular Expressions in Python

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…
Victor Yan
  • 2,778
  • 2
  • 20
  • 25
13
votes
1 answer

Can I use Perl regular expressions to match balanced text?

I would like to match text enclosed in brackets etc in Perl. How can I do that? This is a question from the official perlfaq. We're importing the perlfaq to Stack Overflow.
perlfaq
  • 1,281
  • 4
  • 15
  • 23
7
votes
1 answer

get inner patterns recursively using regex c#

i know there are several questions about regex recursion in .net. I can write somewhat complex regex expressions but this recursion is beyond me, i am just not able to write it. This are the questions closest to what i want. first question, second…
Alex J
  • 1,497
  • 2
  • 25
  • 41
6
votes
2 answers

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

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…
Daniel Vandersluis
  • 83,484
  • 18
  • 156
  • 151
3
votes
1 answer

How to fix a BBcode regular expression

I have a regular expression that grabs BBcode tags. It works great except for a minor glitch. Here is the current expression: \[([^=\[\]]+)[=\x22']*([^ \[\]]*)['\x22]*\](.+)\[/\1\] Here is some text it successfully matches against and the groups it…
Chev
  • 54,842
  • 60
  • 203
  • 309
3
votes
2 answers

Conditions on recursive XPath

How can I use recursive AND conditional selection in XPath? For example, given this document:
jbenet
  • 2,959
  • 3
  • 20
  • 24
2
votes
5 answers

parsing string into array {{navigation({"class": "navigation", "id": "navigation"})}} part 2

Let simplify the question: All I need is to explode() string by a comma between brackets. The problem is that elements selected by comma can have a comma in itself, thus simple exploding won't work. I am not asking how to decode JSON. The number of…
Gajus
  • 55,791
  • 58
  • 236
  • 384
2
votes
1 answer

Regex to parse functions with arbitrary depth

I'm parsing a simple language (Excel formulas) for the functions contained within. A function name must start with any letter, followed by any number of letters/numbers, and ending with an open paren (no spaces in between). For example MyFunc(. …
SFun28
  • 32,209
  • 43
  • 123
  • 233
2
votes
2 answers

Nested regex... I'm clueless!

I'm pretty clueless when it comes to PHP and regex but I'm trying to fix a broken plugin for my forum. I'd like to replace the following:
foo
With
blah
van00
  • 23
  • 3
2
votes
2 answers

Regular expression crashes Apache due to PCRE limitations

I am currently creating bbcode parsing engine and I have encountered a situation what I can't figure out on my own. The thing is, that I popped into a problem exactly like this one: Apache / PHP on Windows crashes with regular expression That means…
Paul
  • 23
  • 3
2
votes
0 answers

Using Regex for URLs with BeautifulSoup?

import urllib2 import re import csv from bs4 import BeautifulSoup def get_BlahBlah(num1, num2, num3, num4): url1 = "http://BlahBlah.com/person_profile/" url2 = "?-id=" url3 = "." url4 = "&source=personalranking=" urlComplete =…
KubiK888
  • 3,525
  • 10
  • 49
  • 89
2
votes
1 answer

Recursive RegEx to match keys and name

I have the strings, ["02-03-2013#3rd Party Fuel", "-1#Archived", "2#06-23-2013#Newswire"], which I want to break down into several parts. These strings are prefixed with date and index keys and contain a name. I've design a RegEx that matches each…
roydukkey
  • 2,813
  • 1
  • 24
  • 36
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
2 3 4