Search code examples
rstringtext

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


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.