nested-reference

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

南楼画角 提交于 2019-12-18 12:54:24
问题 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)++ . "; for (int n = 0; n < 1000; n++) { String s = new String(new char[n]); if (s.matches(FIBONACCI)) { System.out.print(n + " "); } } // 0 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 A possessive repetition (i.e. ++ on the main "loop") is crucial, because you don't

How does this regex replacement reverse a string?

白昼怎懂夜的黑 提交于 2019-11-29 11:38:02
问题 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 be used to reverse a string. The programmatically generated pattern uses meta-pattern abstractions (see: How does this Java regex detect palindromes?). For the first time in the series, these techniques are used for replacement instead of whole

How does this PCRE pattern detect palindromes?

耗尽温柔 提交于 2019-11-27 21:16:46
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 page. Examine this PCRE pattern in PHP snippet: $palindrome = '/(?x) ^ (?: (.) (?= .* ( \1 (?(2) \2 | ) ) $ ) )* .? \2? $ /'; This pattern seems to detect palindromes, as seen in this test cases ( see also on ideone.com ): $tests = array( # palindromes '', 'a', 'aa', 'aaa', 'aba', 'aaaa', 'abba', 'aaaaa', 'abcba', 'ababa', # non-palindromes 'aab', 'abab',

How does this regex find triangular numbers?

与世无争的帅哥 提交于 2019-11-27 17:10:27
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 ways to check if a number is triangular. There's this interesting technique that uses regular expressions as follows: Given n , we first create a string of length n filled with the same character We then match this string against the pattern ^(\1.|^.)+$ n is triangular if and only if this pattern matches the string Here are some snippets to show that this

How does this Java regex detect palindromes?

江枫思渺然 提交于 2019-11-27 13:34:26
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" mechanism is further elaborated upon). This part introduces a specific form of nested assertion, which when combined with nested references allows Java regex to match what most people believe is "impossible": palindromes!! The language of palindromes is non- regular ; it's actually context-free (for a given alphabet). That said, modern regex

How does this PCRE pattern detect palindromes?

我是研究僧i 提交于 2019-11-26 20:35:15
问题 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 page. Examine this PCRE pattern in PHP snippet: $palindrome = '/(?x) ^ (?: (.) (?= .* ( \1 (?(2) \2 | ) ) $ ) )* .? \2? $ /'; This pattern seems to detect palindromes, as seen in this test cases (see also on ideone.com): $tests = array( # palindromes '', 'a

How does this regex find triangular numbers?

泄露秘密 提交于 2019-11-26 18:55:47
问题 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 ways to check if a number is triangular. There's this interesting technique that uses regular expressions as follows: Given n , we first create a string of length n filled with the same character We then match this string against the pattern ^(\1.|^.)+$ n

How does this Java regex detect palindromes?

房东的猫 提交于 2019-11-26 16:25:11
问题 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" mechanism is further elaborated upon). This part introduces a specific form of nested assertion, which when combined with nested references allows Java regex to match what most people believe is "impossible": palindromes!! The language of palindromes

How can we match a^n b^n with Java regex?

て烟熏妆下的殇ゞ 提交于 2019-11-25 23:48:10
问题 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 a n b n . Nested references are first introduced in: How does this regex find triangular numbers? One of the archetypal non-regular languages is: L = { a n b n : n > 0 } This is the language of all non-empty strings consisting of some number of a \'s followed by an equal number of b \'s. Examples of strings in this language are ab , aabb