Quoting Marc Girondot via R-help :
Hi everybody,
I have some questions about the way that sub is working. I hope that
someone has the answer:
1/ Why the second example does not return an empty string ? There is
no match.
subtext <- "-1980-"
sub(".*(1980).*", "\\1", subtext) # return
I answer myself to the third point:
This pattern is better :
pattern.year <- ".*\\b(18|19|20)([0-9][0-9])\\b.*"
subtext <- "bla 1880 bla"
sub(pattern.year, "\\1\\2", subtext) # return 1880
subtext <- "bla 1980 bla"
sub(pattern.year, "\\1\\2", subtext) # return 1980
subtext <- "bla 2010 bla"
So there is probably a command that resets the capture variables as I call
them. No doubt someone will write what it is.
On 9 Aug 2018 10:36, "john matthew" wrote:
> Hi Marc.
> For question 1.
> I know in Perl that regular expressions when captured can be saved if not
> overwritten. \\1 is the
Hi Marc.
For question 1.
I know in Perl that regular expressions when captured can be saved if not
overwritten. \\1 is the capture variable in your R examples.
So the 2nd regular expression does not match but \\1 still has 1980
captured from the previous expression, hence the result.
Maybe if
I answer myself to the third point:
This pattern is better to get a year:
pattern.year <- ".*\\b(18|19|20)([0-9][0-9])\\b.*"
subtext <- "bla 1880 bla"
sub(pattern.year, "\\1\\2", subtext) # return 1880
subtext <- "bla 1980 bla"
sub(pattern.year, "\\1\\2", subtext) # return 1980
subtext <- "bla
Hi everybody,
I have some questions about the way that sub is working. I hope that
someone has the answer:
1/ Why the second example does not return an empty string ? There is no
match.
subtext <- "-1980-"
sub(".*(1980).*", "\\1", subtext) # return 1980
sub(".*(1981).*", "\\1", subtext) #
6 matches
Mail list logo