I have a variable x that is a series of characters such as:
"W1W", "BT3", "BS5", "E1W", "B68"
From this I need to extract the characters before the first numeric character to get e.g.
"W", "BT", "BS", "E", "B"
I have tried looking through previous questions and found:
gsub("[^a-zA-Z]", "", x)
but this keeps the text characters following the numeric character and results in:
"WW", "BT", "BS", "EW", "B"
Is there any way to get only the leading text characters before the numeric character and drop everything afterwards?
You may use
sub("^(\\D+).*", "\\1", x)
If there must be a digit and the digits can be at the start (and you need empty values then), use
sub("^(\\D*)\\d.*", "\\1", x)
See the regex demo and regex demo #2
The regex matches
^
- start of string(\D*)
- 0+ non-digit symbol\d
- a digit.*
- any 0+ chars to the end of the stringUsing x
in the Note at the end, remove everything from the first digit onwards:
sub("\\d.*", "", x)
## [1] "W" "BT" "BS" "E" "B"
x <- c("W1W", "BT3", "BS5", "E1W", "B68")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With