capturing-group

Regex .NET attached named group

风流意气都作罢 提交于 2019-12-11 06:07:34
问题 I want to get attached named group. Source text: 1/2/3/4/5|id1:value1|id2:value2|id3:value3|1/4/2/7/7|id11:value11|id12:value12| Group1: 1/2/3/4/5|id1:value1|id2:value2|id3:value3| Sub groups: id1:value1| id2:value2| id3:value3| Group2: 1/4/2/7/7|id11:value11|id12:value12| Sub groups: id11:value11| id12:value12| How I can do this? 回答1: While this task is easy enough without the complication by splitting, .Net regex matches hold a record of all captures of every group (unlike any other flavor

Javascript RegExp non-capturing groups

[亡魂溺海] 提交于 2019-12-10 12:36:56
问题 I am writing a set of RegExps to translate a CSS selector into arrays of ids and classes. For example, I would like '#foo#bar' to return ['foo', 'bar']. I have been trying to achieve this with "#foo#bar".match(/((?:#)[a-zA-Z0-9\-_]*)/g) but it returns ['#foo', '#bar'], when the non-capturing prefix ?: should ignore the # character. Is there a better solution than slicing each one of the returned strings? 回答1: You could use .replace() or .exec() in a loop to build an Array. With .replace() :

Vim / sed regex backreference in search pattern

房东的猫 提交于 2019-12-07 13:30:46
问题 Vim help says that: \1 Matches the same string that was matched by */\1* *E65* the first sub-expression in \( and \). {not in Vi} Example: "\([a-z]\).\1" matches "ata", "ehe", "tot", etc. It looks like the backreference can be used in search pattern. I started playing with it and I noticed behavior that I can't explain. This is my file: <paper-input label="Input label"> Some text </paper-input> <paper-input label="Input label"> Some text </paper-inputa> <aza> Some text </az> <az> Some text <

Regex - Saving Repeating Captured Group

拈花ヽ惹草 提交于 2019-12-07 05:05:50
问题 This is what I'm doing a = "%span.rockets#diamonds.ribbons.forever" a = a.match(/(^\%\w+)([\.|\#]\w+)+/) puts a.inspect This is what I get #<MatchData "%span.rockets#diamonds.ribbons.forever" 1:"%span" 2:".forever"> This is what I want #<MatchData "%span.rockets#diamonds.ribbons.forever" 1:"%span" 2:".rockets" 3:".#diamonds" 4:".ribbons" 5:".forever"> help? I tried and failed :( 回答1: Generally, you can't get an arbitrary number of capturing groups, but if you use scan you can get a match for

Vim / sed regex backreference in search pattern

非 Y 不嫁゛ 提交于 2019-12-05 20:23:59
Vim help says that: \1 Matches the same string that was matched by */\1* *E65* the first sub-expression in \( and \). {not in Vi} Example: "\([a-z]\).\1" matches "ata", "ehe", "tot", etc. It looks like the backreference can be used in search pattern. I started playing with it and I noticed behavior that I can't explain. This is my file: <paper-input label="Input label"> Some text </paper-input> <paper-input label="Input label"> Some text </paper-inputa> <aza> Some text </az> <az> Some text </az> <az> Some text </aza> I wanted to match the lines where the opening and closing tags are matching i

Regex - Saving Repeating Captured Group

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 10:30:53
This is what I'm doing a = "%span.rockets#diamonds.ribbons.forever" a = a.match(/(^\%\w+)([\.|\#]\w+)+/) puts a.inspect This is what I get #<MatchData "%span.rockets#diamonds.ribbons.forever" 1:"%span" 2:".forever"> This is what I want #<MatchData "%span.rockets#diamonds.ribbons.forever" 1:"%span" 2:".rockets" 3:".#diamonds" 4:".ribbons" 5:".forever"> help? I tried and failed :( Generally, you can't get an arbitrary number of capturing groups, but if you use scan you can get a match for every token you want to capture: a = "%span.rockets#diamonds.ribbons.forever" a = a.scan(/^%\w+|\G[.|#]\w+/)

PHP How to set the preg-groups to “non-capture” (?:…)

折月煮酒 提交于 2019-12-04 20:24:26
In HTML page, I remove HTML comments like this $contentHTML = preg_replace("#(?=<!--)([\s\S]*?)-->#", "", $contentHTML); But on a huge page for preg_replace , I got " PHP Fatal error: Allowed memory size ... " Perhaps, one solution, would use the non-matching group to avoid capturing text? Could someone explain how use on-matching group ?: Or how can I suppress HTML comments in huge page without preg_replace ? Just unroll the regex as $contentHTML = preg_replace("#<!--[^-]*(?:-(?!->)[^-]*)*-->#", "", $contentHTML); See the regex demo . Comapre with yours taking about 3 times as more steps as

what does this django regex mean? `?P`

半世苍凉 提交于 2019-12-03 02:04:31
问题 I have the following regex in my urls.py and I'd like to know what it means. Specifically the (?P<category_slug> portion of the regex. r'^category/(?P<category_slug>[-\w]+)/$ 回答1: (?P<name>regex) - Round brackets group the regex between them. They capture the text matched by the regex inside them that can be referenced by the name between the sharp brackets. The name may consist of letters and digits. Copy paste from: http://www.regular-expressions.info/refext.html 回答2: In django, named

Regex fails to capture all groups

社会主义新天地 提交于 2019-12-02 21:47:33
问题 Using java.util.regex (jdk 1.6), the regular expression 201210(\d{5,5})Test applied to the subject string 20121000002Test only captures group(0) and does not capture group(1) (the pattern 00002 ) as it should, given the code below: Pattern p1 = Pattern.compile("201210(\\d{5,5})Test"); Matcher m1 = p1.matcher("20121000002Test"); if(m1.find()){ for(int i = 1; i<m1.groupCount(); i++){ System.out.println("number = "+m1.group(i)); } } Curiously, another similar regular expression like 201210(\d{5

Regex - capture all repeated iteration

▼魔方 西西 提交于 2019-12-02 03:54:29
I have a variable like this var = "!123abcabc123!" i'm trying to capture all the '123' and 'abc' in this var. this regex (abc|123) retrieve what i want but... My question is: when i try this regex !(abc|123)*! it retrieve only the last iteration. what will i do to get this output MATCH 1 1. [1-4] `123` MATCH 2 1. [4-7] `abc` MATCH 3 1. [7-10] `abc` MATCH 4 1. [10-13] `123` https://regex101.com/r/mD4vM8/3 Thank you!! If your language supports \G then you may free to use this. (?:!|\G(?!^))\K(abc|123)(?=(?:abc|123)*!) DEMO 来源: https://stackoverflow.com/questions/31992558/regex-capture-all