capture-group

python re.sub - alternative replacement patterns

最后都变了- 提交于 2019-11-28 02:12:45
I want to provide alternative replacement patterns to re.sub. Let's say i've got two search patterns as alternatives, like this: re.sub(r"[A-Z]+|[a-z]+", replacementpattern, string) and instead of providing one replacement pattern I would like to somehow catch which search pattern alternative was matched and provide alternative replacement patterns. Is this possible? Thanks. PS. code specifics here are irrelevant, it's a general question. You can pass a function to re.sub() . In the function you can return the value needed based on the captured group. A simple code for illustration: >>> def

python re.sub - alternative replacement patterns

六眼飞鱼酱① 提交于 2019-11-27 08:20:28
问题 I want to provide alternative replacement patterns to re.sub. Let's say i've got two search patterns as alternatives, like this: re.sub(r"[A-Z]+|[a-z]+", replacementpattern, string) and instead of providing one replacement pattern I would like to somehow catch which search pattern alternative was matched and provide alternative replacement patterns. Is this possible? Thanks. PS. code specifics here are irrelevant, it's a general question. 回答1: You can pass a function to re.sub() . In the

Capture groups not working in NSRegularExpression

懵懂的女人 提交于 2019-11-27 00:47:41
问题 Why is this code only spitting out the entire regex match instead of the capture group? Input @"A long string containing Name:</td><td>A name here</td> amongst other things" Output expected A name here Actual output Name:</td><td>A name here</td> Code NSString *htmlString = @"A long string containing Name:</td><td>A name here</td> amongst other things"; NSRegularExpression *nameExpression = [NSRegularExpression regularExpressionWithPattern:@"Name:</td>.*\">(.*)</td>" options

Are non-capturing groups redundant?

你说的曾经没有我的故事 提交于 2019-11-26 23:16:27
Are optional non-capturing groups redundant? Is the following regex: (?:wo)?men semantically equivalent to the following regex? (wo)?men 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 sometimes difficult to use backreferences higher than 9) To avoid the problem with 99 numbered

Regex - Repeating Capturing Group

做~自己de王妃 提交于 2019-11-26 21:19:41
问题 I'm trying to figure out how I can repeat a capture group on the comma-separated values in this the following url string: id=1,2;name=user1,user2,user3;city=Oakland,San Francisco,Seattle;zip=94553,94523; I'm using this RegExp which is return results I want, except for the values since they're dynamic ie. could be 2,3,4,etc users in the url parameter and was wondering if I could create a capture group for each value instead of user1,user2,user3 as one capture-group. RegExp: (^|;|:)(\w+)=([^;]+

Regex group capture in R with multiple capture-groups

青春壹個敷衍的年華 提交于 2019-11-26 15:53:21
In R, is it possible to extract group capture from a regular expression match? As far as I can tell, none of grep , grepl , regexpr , gregexpr , sub , or gsub return the group captures. I need to extract key-value pairs from strings that are encoded thus: \((.*?) :: (0\.[0-9]+)\) I can always just do multiple full-match greps, or do some outside (non-R) processing, but I was hoping I can do it all within R. Is there's a function or a package that provides such a function to do this? str_match() , from the stringr package, will do this. It returns a character matrix with one column for each

Regex group capture in R with multiple capture-groups

徘徊边缘 提交于 2019-11-26 05:59:55
问题 In R, is it possible to extract group capture from a regular expression match? As far as I can tell, none of grep , grepl , regexpr , gregexpr , sub , or gsub return the group captures. I need to extract key-value pairs from strings that are encoded thus: \\((.*?) :: (0\\.[0-9]+)\\) I can always just do multiple full-match greps, or do some outside (non-R) processing, but I was hoping I can do it all within R. Is there\'s a function or a package that provides such a function to do this? 回答1: