Remove numbers from alphanumeric characters

RegexR

Regex Problem Overview


I have a list of alphanumeric characters that looks like:

x <-c('ACO2', 'BCKDHB456', 'CD444')

I would like the following output:

x <-c('ACO', 'BCKDHB', 'CD')

Any suggestions?

# dput(tmp2)

structure(c(432L, 326L, 217L, 371L, 179L, 182L, 188L, 268L, 255L,..., 
), class = "factor")

Regex Solutions


Solution 1 - Regex

You can use gsub for this:

gsub('[[:digit:]]+', '', x)

or

gsub('[0-9]+', '', x)
# [1] "ACO"    "BCKDHB" "CD" 

Solution 2 - Regex

If your goal is just to remove numbers, then the removeNumbers() function removes numbers from a text. Using it reduces the risk of mistakes.

library(tm)

x <-c('ACO2', 'BCKDHB456', 'CD444') 

x <- removeNumbers(x)

x

[1] "ACO"    "BCKDHB" "CD"    

Solution 3 - Regex

Using stringr

Most stringr functions handle regex

str_replace_all will do what you need

str_replace_all(c('ACO2', 'BCKDHB456', 'CD444'), "[:digit:]", "")

Solution 4 - Regex

A solution using stringi:

# your data
x <-c('ACO2', 'BCKDHB456', 'CD444')

# extract capital letters
x <- stri_extract_all_regex(x, "[A-Z]+")

# unlist, so that you have a vector
x <- unlist(x)

Solution in one line:

Screenshot on-liner in R

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBfu38View Question on Stackoverflow
Solution 1 - RegexJustinView Answer on Stackoverflow
Solution 2 - RegexCleonidasView Answer on Stackoverflow
Solution 3 - RegexD BoltaView Answer on Stackoverflow
Solution 4 - RegexaltabqView Answer on Stackoverflow