capture-group

Is there an equivalent of “&” in R's regular expressions for backreference to entire match?

狂风中的少年 提交于 2020-05-08 08:14:49
问题 When I use vim, I often use & to backreference the entire match within substitutions. For example, the following replaces all instances of "foo" with "foobar": %s/foo/&bar/g The benefit here is laziness: I don't have to type the parenthesis in the match and I only have to type one character instead of two for the backreference in the substitution. Perhaps more importantly, I don't have figure out my backrefrences while I'm typing my match, reducing cognitive load. Is there an equivalent to

Is there an equivalent of “&” in R's regular expressions for backreference to entire match?

人走茶凉 提交于 2020-05-08 08:13:26
问题 When I use vim, I often use & to backreference the entire match within substitutions. For example, the following replaces all instances of "foo" with "foobar": %s/foo/&bar/g The benefit here is laziness: I don't have to type the parenthesis in the match and I only have to type one character instead of two for the backreference in the substitution. Perhaps more importantly, I don't have figure out my backrefrences while I'm typing my match, reducing cognitive load. Is there an equivalent to

Is there an equivalent of “&” in R's regular expressions for backreference to entire match?

丶灬走出姿态 提交于 2020-05-08 08:12:29
问题 When I use vim, I often use & to backreference the entire match within substitutions. For example, the following replaces all instances of "foo" with "foobar": %s/foo/&bar/g The benefit here is laziness: I don't have to type the parenthesis in the match and I only have to type one character instead of two for the backreference in the substitution. Perhaps more importantly, I don't have figure out my backrefrences while I'm typing my match, reducing cognitive load. Is there an equivalent to

Why is my regex capture group only capturing the last part of the string when it matches multiple parts?

北慕城南 提交于 2020-01-03 18:57:08
问题 What I Tried var test = "asdfdas ABCD EFGH"; var regex = /^\S+( [A-Z]{4})+$/; // Also tried: /^\S+( [A-Z]{4})+$/g // And: /^\S+( [A-Z]{4})+?$/g var matches = test.match(regex); I made a JSFiddle. What I Expect The variable matches should become this array: [ "asdfdas ABCD EFGH", " ABCD", " EFGH" ] What I Get The variable matches is actually this array: [ "asdfdas ABCD EFGH", " EFGH" ] My Thoughts My guess is that there's something I'm missing with the capture group and/or $ logic. Any help

Java Regex - capture string with single dollar, but not when it has two successive ones

一笑奈何 提交于 2020-01-02 08:16:22
问题 I posted this question earlier. But that wasn't quite the end of it. All the rules that applied there still apply. So the strings: "%ABC%" would yield ABC as a result (capture stuff between percent signs) as would "$ABC." (capture stuff after $, giving up when another dollar or dot appears) "$ABC$XYZ" would too, and also give XYZ as a result. To add a bit more to this: "${ABC}" should yield ABC too. (ignore curly braces if present - non capture chars perhaps?). if you have two successive

optional capture groups with NSRegularExpressions in swift

天大地大妈咪最大 提交于 2019-12-25 07:32:06
问题 I want to have multiple capture groups that can be optional and I want to access the strings they correspond to. Something that looks/works like this: let text1 = "something with foo and bar" let text2 = "something with just bar" let regex = NSRegularExpression(pattern: "(foo)? (bar)") for (first?, second) in regex.matches(in:text1) { print(first) // foo print(second) // bar } for (first?, second) in regex.matches(in:text2) { print(first) // nil print(second) // bar } 回答1: Retrieving captured

How to capture 'multiple' repeated groups with Regular Expressions

感情迁移 提交于 2019-12-23 14:38:10
问题 I have the following text file I would like to parse out to get the individual fields: host_group_web = ( ) host_group_lbnorth = ( lba050 lbhou002 lblon003 ) The fields that I would like to extract are in bold host_group_ web = ( ) host_group_ lbnorth = ( lba505 lbhou002 lblon003 ) host_group_web has no items in between the ( ) , so that portion would be ignored I've named the first group as nodegroup and the items in between the () as nodes I am reading the file line by line, and storing the

How to get capturing group functionality in Go regular expressions

百般思念 提交于 2019-12-17 15:38:21
问题 I'm porting a library from Ruby to Go, and have just discovered that regular expressions in Ruby are not compatible with Go (google RE2). It's come to my attention that Ruby & Java (plus other languages use PCRE regular expressions (perl compatible, which supports capturing groups)), so I need to re-write my expressions so that they compile ok in Go. For example, I have the following regex: `(?<Year>\d{4})-(?<Month>\d{2})-(?<Day>\d{2})` This should accept input such as: 2001-01-20 The

Are non-capturing groups redundant?

大城市里の小女人 提交于 2019-12-17 04:37:09
问题 Are optional non-capturing groups redundant? Is the following regex: (?:wo)?men semantically equivalent to the following regex? (wo)?men 回答1: Your (?:wo)?men and (wo)?men are semantically equivalent, but technically are different, namely, the first is using a non-capturing and the other a capturing group. Thus, the question is why use non-capturing groups when we have capturing ones ? Non-caprturing groups are of help sometimes. To avoid excessive number of backreferences (remember that it is

Regular Expression For Consecutive Duplicate Words

醉酒当歌 提交于 2019-12-16 20:13:36
问题 I'm a regular expression newbie, and I can't quite figure out how to write a single regular expression that would "match" any duplicate consecutive words such as: Paris in the the spring. Not that that is related. Why are you laughing? Are my my regular expressions THAT bad?? Is there a single regular expression that will match ALL of the bold strings above? 回答1: Try this regular expression: \b(\w+)\s+\1\b Here \b is a word boundary and \1 references the captured match of the first group. 回答2