lookaround

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 does the regular expression ‘(?<=#)[^#]+(?=#)’ work?

江枫思渺然 提交于 2019-11-26 13:22:13
I have the following regex in a C# program, and have difficulties understanding it: (?<=#)[^#]+(?=#) I'll break it down to what I think I understood: (?<=#) a group, matching a hash. what's `?<=`? [^#]+ one or more non-hashes (used to achieve non-greediness) (?=#) another group, matching a hash. what's the `?=`? So the problem I have is the ?<= and ?< part. From reading MSDN, ?<name> is used for naming groups, but in this case the angle bracket is never closed. I couldn't find ?= in the docs, and searching for it is really difficult, because search engines will mostly ignore those special

How does the regular expression ‘(?<=#)[^#]+(?=#)’ work?

爱⌒轻易说出口 提交于 2019-11-26 03:40:23
问题 I have the following regex in a C# program, and have difficulties understanding it: (?<=#)[^#]+(?=#) I\'ll break it down to what I think I understood: (?<=#) a group, matching a hash. what\'s `?<=`? [^#]+ one or more non-hashes (used to achieve non-greediness) (?=#) another group, matching a hash. what\'s the `?=`? So the problem I have is the ?<= and ?< part. From reading MSDN, ?<name> is used for naming groups, but in this case the angle bracket is never closed. I couldn\'t find ?= in the

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

Regex lookahead, lookbehind and atomic groups

假如想象 提交于 2019-11-25 21:42:15
问题 I found these things in my regex body but I haven\'t got a clue what I can use them for. Does somebody have examples so I can try to understand how they work? (?!) - negative lookahead (?=) - positive lookahead (?<=) - positive lookbehind (?<!) - negative lookbehind (?>) - atomic group 回答1: Examples Given the string foobarbarfoo : bar(?=bar) finds the 1st bar ("bar" which has "bar" after it) bar(?!bar) finds the 2nd bar ("bar" which does not have "bar" after it) (?<=foo)bar finds the 1st bar