capture-group

How to split a string into two parts then join them in reverse order as a new string?

人盡茶涼 提交于 2019-12-12 03:55:00
问题 This is an example: $str="this is string 1 / 4w"; $str=preg_replace(?); var_dump($str); I want to capture 1 / 4w in this string and move this portion to the begin of string. Result: 1/4W this is string Just give me the variable that contains the capture. The last portion 1 / 4W may be different. e.g. 1 / 4w can be 1/ 16W , 1 /2W , 1W , or 2w The character W may be an upper case or a lower case. 回答1: Use capture group if you want to capture substring: $str = "this is string 1 / 4w"; // "1 / 4w

Can't use '\1' backreference to capture-group in a function call in re.sub() repr expression

巧了我就是萌 提交于 2019-12-11 06:00:41
问题 I have a string S = '02143' and a list A = ['a','b','c','d','e'] . I want to replace all those digits in 'S' with their corresponding element in list A . For example, replace 0 with A[0] , 2 with A[2] and so on. Final output should be S = 'acbed' . I tried: S = re.sub(r'([0-9])', A[int(r'\g<1>')], S) However this gives an error ValueError: invalid literal for int() with base 10: '\\g<1>' . I guess it is considering backreference '\g<1>' as a string. How can I solve this especially using re

Perl REGEX repetive capture groups when post processing output file

旧巷老猫 提交于 2019-12-11 03:45:21
问题 I am dealing with a string of numbers in scientific format. for example 24 6.924E+06 8.316E-01 1.052E-01 1.622E+01 1.311E+01 0.000E+00 6.059E-06 (snip.. extends for a bit) Now I want to write a regex for perl which allows me to capture the ith value in the list. So my current set up is the folloiwng $_ =~ ^\s+\d+\s+(\d+[.]\d+E[+]\d+); my $temp = $1; Which will get me the first number. I want to be able to capture the 7th or the 50th if I wanted without having to write a really long regex

Identifying capture groups in a Regex Pattern

佐手、 提交于 2019-12-10 15:46:33
问题 Is there a way in Java (perhaps with an additional Open Source library) to identify the capture groups in a java.util.regex.Pattern (i.e. before creating a Matcher) Example from the Java docs: Capturing groups are numbered by counting their opening parentheses from left to right. In the expression ((A)(B(C))), for example, there are four such groups: 1 ((A)(B(C))) 2 (A) 3 (B(C)) 4 (C) In principle it should be possible to identify these from the (compiled) Pattern. UPDATE: From @Leniel and

Ruby regular expression matching enumerator with named capture support

别等时光非礼了梦想. 提交于 2019-12-08 08:24:32
问题 Consider a string like this to extract the time information: str = "Sun rises at 6:23 am & sets at 5:45 pm; Moon comes up by 7:20 pm and goes down by 3:45 am" I wish to have an enumerator like scan but one that can get me the MatchData objects instead of arrays as available from scan. For instance, I can write: str.scan( /(?<time>\d:\d{2}) (?<meridiem>am|pm)/ ){ |arr| p arr } to get: ["6:23", "am"] ["5:45", "pm"] ["7:20", "pm"] ["3:45", "am"] But, I wonder if there something like this: str

Code to parse capture groups in regular expressions into a tree

不打扰是莪最后的温柔 提交于 2019-12-08 06:41:00
问题 I need to identify (potentially nested) capture groups within regular expressions and create a tree. The particular target is Java-1.6 and I'd ideally like Java code. A simple example is: "(a(b|c)d(e(f*g))h)" which would be parsed to "a(b|c)d(e(f*g))h" ... "b|c" ... "e(f*g)" ... "f*g" The solution should ideally account for count expressions, quantifiers, etc and levels of escaping. However if this is not easy to find a simpler approach might suffice as we can limit the syntax used. EDIT . To

Ruby regular expression matching enumerator with named capture support

可紊 提交于 2019-12-07 06:51:26
Consider a string like this to extract the time information: str = "Sun rises at 6:23 am & sets at 5:45 pm; Moon comes up by 7:20 pm and goes down by 3:45 am" I wish to have an enumerator like scan but one that can get me the MatchData objects instead of arrays as available from scan. For instance, I can write: str.scan( /(?<time>\d:\d{2}) (?<meridiem>am|pm)/ ){ |arr| p arr } to get: ["6:23", "am"] ["5:45", "pm"] ["7:20", "pm"] ["3:45", "am"] But, I wonder if there something like this: str.match_all( /(?<time>\d:\d{2}) (?<meridiem>am|pm)/ ){ |md| p md } to get: #<MatchData "6:23 am" time:"6:23

Expected outcome in group capture?

元气小坏坏 提交于 2019-12-06 14:05:42
问题 String line = "This order was placed for QT3000! OK?"; String pattern = "(.*)(\\d+)(.*)"; // Create a Pattern object Pattern r = Pattern.compile(pattern); // Now create matcher object. Matcher m = r.matcher(line); if (m.find()) { System.out.println("Found value: " + m.group(1)); System.out.println("Found value: " + m.group(2)); System.out.println("Found value: " + m.group(3)); } output is Found value: This order was placed for QT300 Found value: 0 Found value: ! OK? Though i was expecting the

Expected outcome in group capture?

僤鯓⒐⒋嵵緔 提交于 2019-12-04 18:53:28
String line = "This order was placed for QT3000! OK?"; String pattern = "(.*)(\\d+)(.*)"; // Create a Pattern object Pattern r = Pattern.compile(pattern); // Now create matcher object. Matcher m = r.matcher(line); if (m.find()) { System.out.println("Found value: " + m.group(1)); System.out.println("Found value: " + m.group(2)); System.out.println("Found value: " + m.group(3)); } output is Found value: This order was placed for QT300 Found value: 0 Found value: ! OK? Though i was expecting the output as Found value: This order was placed for QT3000! OK? Found value: 3000 Found value: This order

C++11 Regex Capture Groups By Name

≯℡__Kan透↙ 提交于 2019-12-04 17:43:19
问题 I am converting my boost-based regular expressions to C++11 regex. I have a capture group called url : \s*?=\s*?(("(?<url>.*?)")|('?<url>.*?)')) With boost, if you had an smatch you could call match.str("url") to get the capture group by name. With std::smatch , I am only seeing indexed sub-matches. How can I get access to the url capture using the std::smatch class? 回答1: You cannot name a capture group with the c++11 standard. C++11 regex conforms to the ECMAScript syntax. Here is a link