[R] Regex question to find a string that contains 5-9 alpha-numeric characters, at least one of which is a number

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue Jun 9 19:06:24 CEST 2009


Greg Snow wrote:
> Here is one way using a single pattern (so can be used in a substitution), it uses Perl's positive look ahead patters:
>
>   
>> test <- c("SHRT","5HRT","M1TCH","M1TCH5","LONG3RS","NONUMBER","TOOLOOOONGG","ooops.3")
>>
>> sub( '(?=[a-zA-Z]{0,8}[0-9])[a-zA-Z0-9]{5,9}', 'xxx', test, perl=TRUE)
>>     


yes, but:

    sub( '(?=[a-zA-Z]{0,8}[0-9])[a-zA-Z0-9]{5,9}', 'xxxxx', '12345',
perl=TRUE)
    # "xxxxx"

which is not what was expected -- as far as i understand, the point was
to match 5-9 character strings with exactly 1 digit.

vQ




More information about the R-help mailing list