String Detecting in R

I have the following strings.

x <- c("A1A1A1", "A3V???", "B4F3**")

I want to flag only the strings in which the last 3 characters do not follow the patter [[:digit:]][[:alpha:]][[:digit]]

Thus, I would want to flag the 2nd and 3rd string above. Any suggestions?

2 answers

  • answered 2018-12-05 20:33 Jilber Urbina

    grepl is suitable here

    > !grepl("\\d\\w\\d$", x)

    If you want to get the position:

    > grep("\\d\\w\\d$", x, invert = TRUE)
    [1] 2 3

  • answered 2018-12-05 20:35 Lasarus9

    Just for clarification, are you trying to remove those strings that do not follow that pattern? The way i can think of doing this is clnstrings <- str_remove_all(vectornameofstrings, "symbols or patterns that you would want removed") There are probably more efficient ways to do this, but from my knowledge (which is limited, as I am still learning) this could be a way to do it. If anyone else has any input on this answer please don't hesitate to comment!