Questions tagged [nested-reference]

A nested reference in regular expressions is a ("back")reference to what a group matched within the definition of that group.

These articles were specifically written to introduce usage of nested reference:

The solutions to these questions use nested reference:

8 questions
108
votes
3 answers

How can we match a^n b^n?

This is the second part of a series of educational regex articles. It shows how lookaheads and nested references can be used to match the non-regular languge anbn. Nested references are first introduced in: How does this regex find triangular…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
44
votes
1 answer

How does this regex find triangular numbers?

Part of a series of educational regex articles, this is a gentle introduction to the concept of nested references. The first few triangular numbers are: 1 = 1 3 = 1 + 2 6 = 1 + 2 + 3 10 = 1 + 2 + 3 + 4 15 = 1 + 2 + 3 + 4 + 5 There are many…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
23
votes
1 answer

How does this Java regex detect palindromes?

This is the third part in a series of educational regex articles. It follows How does this regex find triangular numbers? (where nested references is first introduced) and How can we match a^n b^n with Java regex? (where the lookahead "counting"…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
20
votes
2 answers

How does this PCRE pattern detect palindromes?

This question is an educational demonstration of the usage of lookahead, nested reference, and conditionals in a PCRE pattern to match ALL palindromes, including the ones that can't be matched by the recursive pattern given in the PCRE man…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
18
votes
2 answers

How does this regex replacement reverse a string?

This is the fourth part in a series of educational regex articles. It show how the combination of nested reference (see: How does this regex find triangular numbers?) to "count" within assertions (see: How can we match a^n b^n with Java regex?) can…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
15
votes
1 answer

Why does Java regex engine throw StringIndexOutOfBoundsException on a + repetition?

I've written a regex pattern to find Fibonacci numbers (it doesn't matter why, I just did). It works wonderfully as expected (see on ideone.com): String FIBONACCI = "(?x) .{0,2} | (?: (?=(\\2?)) (?=(\\2\\3|^.)) (?=(\\1)) \\2)++ . "; …
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
4
votes
2 answers

Regexercise: factorials

This is an experimental new feature for StackOverlow: exercising your regex muscles by solving various classical problems. There is no one right answer, and in fact we should collect as many right answers as possible, as long as they offer…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
0
votes
1 answer

Perl nested structure: recursive function

As a follow up to my previous post here! I tested the algorithm with nested hash references: Algorithm: use strict; use warnings; &expand_references2([a,b,{c=>123},d]); sub expand_references2 { my $indenting = -1; my $inner; $inner = sub { …