matching certain numbers at the end of a string

Posted by user697473 on Stack Overflow See other posts from Stack Overflow or by user697473
Published on 2012-11-21T22:08:09Z Indexed on 2012/11/21 22:59 UTC
Read the original article Hit count: 165

Filed under:
|

I have a vector of strings:

s <- c('abc1',   'abc2',   'abc3',   'abc11',   'abc12', 
       'abcde1', 'abcde2', 'abcde3', 'abcde11', 'abcde12', 
       'nonsense')

I would like a regular expression to match only the strings that begin with abc and end with 3, 11, or 12. In other words, the regex has to exclude abc1 but not abc11, abc2 but not abc12, and so on.

I thought that this would be easy to do with lookahead assertions, but I haven't found a way. Is there one?


EDIT: Thanks to posters below for pointing out a serious ambiguity in the original post.

In reality, I have many strings. They all end in digits: some in 0, some in 9, some in the digits in between. I am looking for a regex that will match all strings except those that end with a letter followed by a 1 or a 2. (The regex should also match only those strings that start with abc, but that's an easy problem.)

I tried to use negative lookahead assertions to create such a regex. But I didn't have any success.

© Stack Overflow or respective owner

Related posts about regex

Related posts about r