Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get Words before/between/after special characters

I have strings like this one x <- "123/456/789". I want to do three different things

  1. Get the first word before /, that is, 123
  2. Get the word between two /: 456
  3. Get the last word, the one afte the last /

I know there are some functions like gsub and packages like stingr but I did not find something useful

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could get it to work using gsub and str_extract from {stringr}:

# Get 123
gsub("/\\d*", "", x)

# Get 456
gsub("/", "", stringr::str_extract(x, "/\\d*/"))

# Get 789
gsub("\\d*/", "", x)

This results in:

[1] "123"

[1] "456"

[1] "789"

In the RegEx, \\d* indicates one or more digits, but if your word consists of other characters you could also use a different indicator.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading