capturing-group

Java Pattern print capturing groups

左心房为你撑大大i 提交于 2019-12-17 19:37:12
问题 ((\d{1,2})/(\d{1,2})/(\d{2,4})) Is there a way to retrieve a list of all the capture groups with the Pattern object. I debugged the object and all it says is how many groups there are (5). I need to retrieve a list of the following capture groups. Example of output: 0 ((\d{1,2})/(\d{1,2})/(\d{2,4})) 1 (\d{2})/(\d{2})/(\d{4}) 2 \d{2} 3 \d{2} 4 \d{4} Update: I am not necessarily asking if a regular expression exists, but that would be most favorable. So far I have created a rudimentary parser

Expected behavior with regular expressions with capturing-groups in pandas' `str.extract()`

牧云@^-^@ 提交于 2019-12-17 17:33:26
问题 I'm trying to get a grasp on regular expressions and I came across with the one included inside the str.extract method: movies['year']=movies['title'].str.extract('.*\((.*)\).*',expand=True) It is supposed to detect and extract whichever is in parentheses. So, if given this string: foobar (1995) it should return 1995 . However, if I open a terminal and type the following echo 'foobar (1995)` | grep '.*\((.*)\).*' matches the whole string instead of only the content between parentheses. I

Expected behavior with regular expressions with capturing-groups in pandas' `str.extract()`

泄露秘密 提交于 2019-12-12 15:51:55
问题 I'm trying to get a grasp on regular expressions and I came across with the one included inside the str.extract method: movies['year']=movies['title'].str.extract('.*\((.*)\).*',expand=True) It is supposed to detect and extract whichever is in parentheses. So, if given this string: foobar (1995) it should return 1995 . However, if I open a terminal and type the following echo 'foobar (1995)` | grep '.*\((.*)\).*' matches the whole string instead of only the content between parentheses. I

How do I use regular expression capturing groups with JFlex?

佐手、 提交于 2019-12-12 14:10:30
问题 Although this question is about JFlex, it probably applies to other scanner generators such as lex, flex as well. If I have some rule, how can I create a capturing group in part of that rule and use the result of that captured group as an argument to the code that gets called upon the rule matching? For example, let's say I had a simple rule to match an SGML tag: "<"[a-zA-Z]+">" {return new Token(Type.OPEN_TAG);} How could I capture the inner character part([a-zA-Z]+) and use it as an

Negative lookahead with capturing groups

人盡茶涼 提交于 2019-12-12 10:45:31
问题 I'm attempting this challenge: https://regex.alf.nu/4 I want to match all strings that don't contain an ABBA pattern. Match: aesthophysiology amphimictical baruria calomorphic Don't Match anallagmatic bassarisk chorioallantois coccomyces abba Firstly, I have a regex to determine the ABBA pattern. (\w)(\w)\2\1 Next I want to match strings that don't contain that pattern: ^((?!(\w)(\w)\2\1).)*$ However this matches everything. If I simplify this by specifying a literal for the negative

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

倾然丶 夕夏残阳落幕 提交于 2019-12-12 09:18:23
问题 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 ? 回答1: Just unroll the regex as $contentHTML = preg_replace("#<!--[^-]*(?:-(?!->)[^-]

replace all captured groups

二次信任 提交于 2019-12-11 20:01:09
问题 I need to transform something like: "foo_bar_baz_2" to "fooBarBaz2" I'm trying to use this Pattern: Pattern pattern = Pattern.compile("_([a-z])"); Matcher matcher = pattern.matcher("foo_bar_baz_2"); Is it possible to use matcher to replace the first captured group (the letter after the '_') with the captured group in upper case? 回答1: You can use appendReplacement/appendTail methods of the matcher like this: Pattern pattern = Pattern.compile("_([a-z0-9])"); Matcher matcher = pattern.matcher(

RegEx: Grabbing value between quotation marks from string

回眸只為那壹抹淺笑 提交于 2019-12-11 07:44:39
问题 This is related to: RegEx: Grabbing values between quotation marks. If there is a String like this: HYPERLINK "hyperlink_funda.docx" \l "Sales" The regex given on the link (["'])(?:(?=(\\?))\2.)*?\1 is giving me [" HYPERLINK ", " \l ", " "] What regex will return values enclosed in quotation mark (specifically between the \" marks) ? ["hyperlink_funda.docx", "Sales"] Using Java, String.split(String regex) way. 回答1: You're not supposed to use that with .split() method. Instead use a Pattern

Regex capturing group doesn't recognise group(1) despite matches() true

核能气质少年 提交于 2019-12-11 06:55:40
问题 I'm writing some simple (I thought) regex in Java to remove an asterisk or ampersand which occurs directly next to some specified punctuation. This was my original code: String ptr = "\\s*[\\*&]+\\s*"; String punct1 = "[,;=\\{}\\[\\]\\)]"; //need two because bracket rules different for ptr to left or right String punct2 = "[,;=\\{}\\[\\]\\(]"; out = out.replaceAll(ptr+"("+punct1+")|("+punct2+")"+ptr,"$1"); Which instead of just removing the "ptr" part of the string, removed the punct too! (i

Java regex: how to back-reference capturing groups in a certain context when their number is not known in advance

柔情痞子 提交于 2019-12-11 06:22:15
问题 As an introductory note, I am aware of the old saying about solving problems with regex and I am also aware about the precautions on processing XML with RegEx. But please bear with me for a moment... I am trying to do a RegEx search and replace on a group of characters. I don't know in advance how often this group will be matched, but I want to search with a certain context only. An example: If I have the following string "**ab**df**ab**sdf**ab**fdsa**ab**bb" and I want to search for "ab" and