capturing-group

Regex optional capturing group?

馋奶兔 提交于 2020-12-24 15:20:50
问题 After hours of searching I decided to ask this question. Why doesn't this regular expression ^(dog).+?(cat)? work as I think it should work (i.e. capture the first dog and cat if there is any)? What am I missing here? dog, cat dog, dog, cat dog, dog, dog 回答1: The reason that you do not get an optional cat after a reluctantly-qualified .+? is that it is both optional and non-anchored: the engine is not forced to make that match, because it can legally treat the cat as the "tail" of the .+?

Regex optional capturing group?

佐手、 提交于 2020-12-24 15:20:39
问题 After hours of searching I decided to ask this question. Why doesn't this regular expression ^(dog).+?(cat)? work as I think it should work (i.e. capture the first dog and cat if there is any)? What am I missing here? dog, cat dog, dog, cat dog, dog, dog 回答1: The reason that you do not get an optional cat after a reluctantly-qualified .+? is that it is both optional and non-anchored: the engine is not forced to make that match, because it can legally treat the cat as the "tail" of the .+?

Why does re.sub replace the entire pattern, not just a capturing group within it?

可紊 提交于 2020-06-08 04:48:46
问题 re.sub('a(b)','d','abc') yields dc , not adc . Why does re.sub replace the entire capturing group, instead of just capturing group'(b)'? 回答1: Because it's supposed to replace the whole occurrence of the pattern: Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. If it were to replace only some subgroup, then complex regexes with several groups wouldn't work. There are several possible solutions: Specify pattern in

Positive lookbehind vs non-capturing group: different behaviuor

安稳与你 提交于 2020-02-27 07:35:23
问题 I use python regular expressions ( re module) in my code and noticed different behaviour in theese cases: re.findall(r'\s*(?:[a-z]\))?[^.)]+', 'a) xyz. b) abc.') # non-capturing group # results in ['a) xyz', ' b) abc'] and re.findall(r'\s*(?<=[a-z]\))?[^.)]+', 'a) xyz. b) abc.') # lookbehind # results in ['a', ' xyz', ' b', ' abc'] What I need to get is just ['xyz', 'abc'] . Why are the examples behave differently and how t get the desired result? 回答1: The reason a and b are included in the

Scala regex Named Capturing Groups

浪子不回头ぞ 提交于 2020-01-09 10:19:52
问题 In scala.util.matching.Regex trait MatchData I see that there support for groupnames , I thought that this was related to (Regex Named Capturing Groups) But since Java does not support groupnames until version 7 as I understand it (ref), Scala version 2.8.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6. gives me this exception: scala> val pattern = """(?<login>\w+) (?<id>\d+)""".r java.util.regex.PatternSyntaxException: Look-behind group does not have an obvio us maximum length near index 11 (

Scala regex Named Capturing Groups

假装没事ソ 提交于 2020-01-09 10:19:11
问题 In scala.util.matching.Regex trait MatchData I see that there support for groupnames , I thought that this was related to (Regex Named Capturing Groups) But since Java does not support groupnames until version 7 as I understand it (ref), Scala version 2.8.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6. gives me this exception: scala> val pattern = """(?<login>\w+) (?<id>\d+)""".r java.util.regex.PatternSyntaxException: Look-behind group does not have an obvio us maximum length near index 11 (

jq add capturing group result outside

拈花ヽ惹草 提交于 2019-12-24 07:03:48
问题 For example, Input: { "id":"abc", "name": "name-middlenane-lastname-1" }, { "id":"123", "name": "fname-flast-2" } response: { "id":"abc", "name": "name-middlename-lastname-1", "newkey": "name-middlename-lastname" }, { "id":"123", "name": "fname-flast-2", "newkey": "fname-flast" } The filed name in each object is a string with characters and numbers separated by "-" hyphen. I need the complete string from beginning till the starting number. I don't want anything which is there after the number

Capturing <thisPartOnly> and (thisPartOnly) with the same group

£可爱£侵袭症+ 提交于 2019-12-23 12:16:51
问题 Let's say we have the following input: <amy> (bob) <carol) (dean> We also have the following regex: <(\w+)>|\((\w+)\) Now we get two matches (as seen on rubular.com): <amy> is a match, \1 captures amy , \2 fails (bob) is a match, \2 captures bob , \1 fails This regex does most of what we want, which are: It matches the open and close brackets properly (i.e. no mixing) It captures the part we're interested in However, it does have a few drawbacks: The capturing pattern (i.e. the "main" part)

Regex - capture all repeated iteration

女生的网名这么多〃 提交于 2019-12-20 04:48:53
问题 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!! 回答1: If your language supports \G then you may free to use this. (?:!|\G(?!^)

Saving substrings using Regular Expressions

我们两清 提交于 2019-12-20 03:36:13
问题 I'm new to regular expressions in Java (or any language, for that matter) and I'm wanting to do a find using them. The tricky part that I don't understand how to do is replace something inside the string that matches. For example, if the line I'm looking for is Person item6 [can {item thing [wrap]}] I'm able to write a regex that finds that line, but finding what the word "thing" is (as it may differ among different lines) is my problem. I may want to either replace that word with something