Skip to content
Prev 355334 / 398500 Next

Regex: Combining sub/grepl with ifelse

David answered most of this. Just a two short notes inline.
On Oct 10, 2015, at 12:38 AM, Omar Andr? Gonz?les D?az <oma.gonzales at gmail.com> wrote:

            
Yes. I am not matching the entire token here. Note there is no "+": The two character-class expressions match exactly one uppercase character adjacent to exactly one number. If this is found in a token, grep returns TRUE. It doesn't matter what else the token contains - the first regex already took care of removing everything that's not needed. The vector of FALSEs and a single TRUE that grep() returns goes inside the square brackets, and selects the token from v.
This can be very confusing about regular expressions: the same character can mean different things depending on where it is found. Between two characters in a character class expresssion, the hyphen means "range". Elsewhere it is a literal hyphen. David put his at the beginning, I had it at the end (in the first regex). Another tricky character is "?" which can mean 0,1 matches, or turn "greedy" matching off... 

Online regex testers are invaluable to develop a regex - one I frequently use is regexpal.com

Cheers,
B.