capturing-group

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

How to capture an arbitrary number of groups in JavaScript Regexp?

戏子无情 提交于 2019-11-25 23:35:12
问题 I would expect this line of JavaScript: \"foo bar baz\".match(/^(\\s*\\w+)+$/) to return something like: [\"foo bar baz\", \"foo\", \" bar\", \" baz\"] but instead it returns only the last captured match: [\"foo bar baz\", \" baz\"] Is there a way to get all the captured matches? 回答1: When you repeat a capturing group, in most flavors, only the last capture is kept; any previous capture is overwritten. In some flavor, e.g. .NET, you can get all intermediate captures, but this is not the case

What is a non-capturing group in regular expressions?

我是研究僧i 提交于 2019-11-25 22:11:10
问题 How are non-capturing groups, i.e. (?:) , used in regular expressions and what are they good for? 回答1: Let me try to explain this with an example. Consider the following text: http://stackoverflow.com/ https://stackoverflow.com/questions/tagged/regex Now, if I apply the regex below over it... (https?|ftp)://([^/\r\n]+)(/[^\r\n]*)? ... I would get the following result: Match "http://stackoverflow.com/" Group 1: "http" Group 2: "stackoverflow.com" Group 3: "/" Match "https://stackoverflow.com