regex-group

python re.sub group: number after \\number

喜夏-厌秋 提交于 2019-11-26 07:24:53
How can I replace foobar with foo123bar ? This doesn't work: >>> re.sub(r'(foo)', r'\1123', 'foobar') 'J3bar' This works: >>> re.sub(r'(foo)', r'\1hi', 'foobar') 'foohibar' I think it's a common issue when having something like \number . Can anyone give me a hint on how to handle this? The answer is: re.sub(r'(foo)', r'\g<1>123', 'foobar') Relevant excerpt from the docs: In addition to character escapes and backreferences as described above, \g will use the substring matched by the group named name, as defined by the (?P...) syntax. \g uses the corresponding group number; \g<2> is therefore

Named regular expression group “(?P<group_name>regexp)”: what does “P” stand for?

扶醉桌前 提交于 2019-11-26 06:13:46
问题 In Python, the (?P<group_name>…) syntax allows one to refer to the matched string through its name: >>> import re >>> match = re.search(\'(?P<name>.*) (?P<phone>.*)\', \'John 123456\') >>> match.group(\'name\') \'John\' What does \"P\" stand for? I could not find any hint in the official documentation. I would love to get ideas about how to help my students remember this syntax. Knowing what \"P\" does stand for (or might stand for) would be useful. 回答1: Since we're all guessing, I might as

How to capture multiple repeated groups?

好久不见. 提交于 2019-11-26 02:37:23
问题 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

python re.sub group: number after \number

拈花ヽ惹草 提交于 2019-11-26 01:37:43
问题 How can I replace foobar with foo123bar ? This doesn\'t work: >>> re.sub(r\'(foo)\', r\'\\1123\', \'foobar\') \'J3bar\' This works: >>> re.sub(r\'(foo)\', r\'\\1hi\', \'foobar\') \'foohibar\' I think it\'s a common issue when having something like \\number . Can anyone give me a hint on how to handle this? 回答1: The answer is: re.sub(r'(foo)', r'\g<1>123', 'foobar') Relevant excerpt from the docs: In addition to character escapes and backreferences as described above, \g will use the substring

RegEx for matching UK Postcodes

泪湿孤枕 提交于 2019-11-25 22:58:15
问题 I\'m after a regex that will validate a full complex UK postcode only within an input string. All of the uncommon postcode forms must be covered as well as the usual. For instance: Matches CW3 9SS SE5 0EG SE50EG se5 0eg WC2H 7LT No Match aWC2H 7LT WC2H 7LTa WC2H How do I solve this problem? 回答1: I'd recommend taking a look at the UK Government Data Standard for postcodes [link now dead; archive of XML, see Wikipedia for discussion]. There is a brief description about the data and the attached

RegEx to extract all matches from string using RegExp.exec

你离开我真会死。 提交于 2019-11-25 22:35:49
问题 I\'m trying to parse the following kind of string: [key:\"val\" key2:\"val2\"] where there are arbitrary key:\"val\" pairs inside. I want to grab the key name and the value. For those curious I\'m trying to parse the database format of task warrior. Here is my test string: [description:\"aoeu\" uuid:\"123sth\"] which is meant to highlight that anything can be in a key or value aside from space, no spaces around the colons, and values are always in double quotes. In node, this is my output:

What is a non-capturing group in regular expressions?

我是研究僧i 提交于 2019-11-25 22:11:10
问题 How are non-capturing groups, i.e. (?:) , used in regular expressions and what are they good for? 回答1: Let me try to explain this with an example. Consider the following text: http://stackoverflow.com/ https://stackoverflow.com/questions/tagged/regex Now, if I apply the regex below over it... (https?|ftp)://([^/\r\n]+)(/[^\r\n]*)? ... I would get the following result: Match "http://stackoverflow.com/" Group 1: "http" Group 2: "stackoverflow.com" Group 3: "/" Match "https://stackoverflow.com