regex-group

Powershell - Regular Expression Multiple Matches

六月ゝ 毕业季﹏ 提交于 2019-11-28 04:34:09
问题 Maybe my reasoning is faulty, but I can't get this working. Here's my regex: (Device\s#\d(\n.*)*?(?=\n\s*Device\s#|\Z)) Try it: http://regex101.com/r/jQ6uC8/6 $getdevice is the input string. I'm getting this string from the Stream/Output from a command line tool. $dstate = $getdevice | select-string -pattern '(Device\s#\d(\n.*)*?(?=\n\s*SSD\s+|\Z))' -AllMatches | % { $_ -match '(Device\s#\d(\n.*)*?(?=\n\s*SSD\s+|\Z))' > $null; $matches[0] } Write-Host $dstate Output: Device #0 Device #1

Can I use an OR in regex without capturing what's enclosed?

可紊 提交于 2019-11-28 03:37:40
I'm using rubular.com to build my regex, and their documentation describes the following: (...) Capture everything enclosed (a|b) a or b How can I use an OR expression without capturing what's in it? For example, say I want to capture either "ac" or "bc". I can't use the regex (a|b)(c) right? Since then I capture either "a" or "b" in one group and "c" in another, not the same. I know I can filter through the captured results, but that seems like more work... Am I missing something obvious? I'm using this in Java, if that is pertinent. Depending on the regular expression implementation you can

Vim Regex Capture Groups [bau -> byau : ceu -> cyeu]

≯℡__Kan透↙ 提交于 2019-11-28 02:56:19
I have a list of words: bau ceu diu fou gau I want to turn that list into: byau cyeu dyiu fyou gyau I unsuccessfully tried the command: :%s/(\w)(\w\w)/\1y\2/g Given that this doesn't work, what do I have to change to make the regex capture groups work in Vim? One way to fix this is by ensuring the pattern is enclosed by escaped parentheses: :%s/\(\w\)\(\w\w\)/\1y\2/g Slightly shorter (and more magic -al) is to use \v , meaning that in the pattern after it all ASCII characters except '0'-'9' , 'a'-'z' , 'A'-'Z' and '_' have a special meaning: :%s/\v(\w)(\w\w)/\1y\2/g See: :help \( :help \v If

How can I store captures from a Perl regular expression into separate variables?

怎甘沉沦 提交于 2019-11-27 14:44:33
问题 I have a regex: /abc(def)ghi(jkl)mno(pqr)/igs How would I capture the results of each parentheses into 3 different variables, one for each parentheses? Right now I using one array to capture all the results, they come out sequential but then I have to parse them and the list could be huge. @results = ($string =~ /abc(def)ghi(jkl)mno(pqr)/igs); 回答1: Your question is a bit ambiguous to me, but I think you want to do something like this: my (@first, @second, @third); while( my ($first, $second,

Can I replace groups in Java regex?

你说的曾经没有我的故事 提交于 2019-11-27 03:22:21
I have this code, and I want to know, if I can replace only groups (not all pattern) in Java regex. Code: //... Pattern p = Pattern.compile("(\\d).*(\\d)"); String input = "6 example input 4"; Matcher m = p.matcher(input); if (m.find()) { //Now I want replace group one ( (\\d) ) with number //and group two (too (\\d) ) with 1, but I don't know how. } Chadwick Use $n (where n is a digit) to refer to captured subsequences in replaceFirst(...) . I'm assuming you wanted to replace the first group with the literal string "number" and the second group with the value of the first group. Pattern p =

Can I use an OR in regex without capturing what's enclosed?

爷,独闯天下 提交于 2019-11-27 00:07:37
问题 I'm using rubular.com to build my regex, and their documentation describes the following: (...) Capture everything enclosed (a|b) a or b How can I use an OR expression without capturing what's in it? For example, say I want to capture either "ac" or "bc". I can't use the regex (a|b)(c) right? Since then I capture either "a" or "b" in one group and "c" in another, not the same. I know I can filter through the captured results, but that seems like more work... Am I missing something obvious? I

Vim Regex Capture Groups [bau -> byau : ceu -> cyeu]

戏子无情 提交于 2019-11-26 23:53:30
问题 I have a list of words: bau ceu diu fou gau I want to turn that list into: byau cyeu dyiu fyou gyau I unsuccessfully tried the command: :%s/(\w)(\w\w)/\1y\2/g Given that this doesn't work, what do I have to change to make the regex capture groups work in Vim? 回答1: One way to fix this is by ensuring the pattern is enclosed by escaped parentheses: :%s/\(\w\)\(\w\w\)/\1y\2/g Slightly shorter (and more magic -al) is to use \v , meaning that in the pattern after it all ASCII characters except '0'-

Negating a backreference in Regular Expressions

断了今生、忘了曾经 提交于 2019-11-26 12:25:14
问题 if a string has this predicted format: value = \"hello and good morning\" Where the \" (quotations) might also be \' (single quote), and the closing char (\' or \") will be the same as the opening one. I want to match the string between the quotation marks. \\bvalue\\s*=\\s*([\"\'])([^\\1]*)\\1 (the two \\s are to allow any spaces near the = sign) The first \"captured group\" (inside the first pair of brackets) - should match the opening quotation which should be either \' or \" then - I\'m

Can I replace groups in Java regex?

◇◆丶佛笑我妖孽 提交于 2019-11-26 10:29:57
问题 I have this code, and I want to know, if I can replace only groups (not all pattern) in Java regex. Code: //... Pattern p = Pattern.compile(\"(\\\\d).*(\\\\d)\"); String input = \"6 example input 4\"; Matcher m = p.matcher(input); if (m.find()) { //Now I want replace group one ( (\\\\d) ) with number //and group two (too (\\\\d) ) with 1, but I don\'t know how. } 回答1: Use $n (where n is a digit) to refer to captured subsequences in replaceFirst(...). I'm assuming you wanted to replace the

How to capture multiple repeated groups?

删除回忆录丶 提交于 2019-11-26 08:42:56
I need to capture multiple groups of the same pattern. Suppose, I have a following string: HELLO,THERE,WORLD And I've written a following pattern ^(?:([A-Z]+),?)+$ What I want it to do is, capture every single word, so that Group 1 is : "HELLO", Group 2 is "THERE" and Group 3 is "WORLD" What my regex is actually capturing only the last one, which is "WORLD". I'm testing my regular expression here and I want to use it with Swift (maybe there's a way in Swift to get intermediate results somehow, so that I can use them?) UPDATE: I don't want to use split . I just need to now how to capture all