tre icon indicating copy to clipboard operation
tre copied to clipboard

Lazy Modifier `?` Works Incorrectly in Capture Groups

Open brodieG opened this issue 10 years ago • 0 comments

This is using the library included in R 3.0.2:

text <- "abcdEEEEfg"

sub("c.+?E", "###", text)
# [1] "ab###EEEfg"                          <<< OKAY
sub("c(.+?)E", "###", text)
# [1] "ab###EEfg"                           <<< WEIRD
sub("c(.+?)E", "###", text, perl=T)
# [1] "ab###EEEfg"                          <<< OKAY

Notice how in the second example something very odd is happening. The capture is neither greedy nor lazy. It should be lazy and look like the first and third examples, but if it were greedy it would capture one more E than it did.

This is almost certainly related to issue 11, but posting it separately as they issue description is not quite the same.

Also, original post on SO for reference with some more details.

brodieG avatar Jun 19 '14 13:06 brodieG