Electrino
Electrino

Reputation: 2890

Remove everything after backslash in base R?

Similar questions have been asked here and here. However, I can't seem to get them to work for me.

If I have a character vector like:

myString <- c("5", "10", "100\abc\nx1\n1")

I want to remove everything after (and including) the first backslash. For example, my expected result would be:

>myString
"5" "10" "100"

I have tried using sub, gsub, and strsplit but I just can't seem to get it to work. Things I've tried:

gsub("\\\\*", "", myString)
sub("\\\\.*", "", myString)
gsub('\\"', "", myString, fixed = TRUE)
gsub("\\.*","", myString)

But I'm not great with regex stuff so I'm almost definitely not using these functions correctly! Any advice as to how I'd fix this?

Upvotes: 2

Views: 517

Answers (3)

akrun
akrun

Reputation: 886968

We could use parse_number

readr::parse_number(myString)
[1]   5  10 100

Upvotes: 1

Anoushiravan R
Anoushiravan R

Reputation: 21908

Here is another way you could try:

gsub("(^\\d+)([\a-zA-Z0-9]*)", "\\1", myString)

[1] "5"   "10"  "100"

Upvotes: 2

Silentdevildoll
Silentdevildoll

Reputation: 1280

Using the information from @Skaqqs, it led me to something helpful by @bartektartanus. It's not base R unfortunately, but I think this should work using the stringi package to escape the uniciode

library(stringi)
myString <- c("5", "10", "100\abc\nx1\n1")
gsub("\\\\.*", "", stri_escape_unicode(myString))

result:

 "5"   "10"  "100"

Upvotes: 1

Related Questions