Extract a regular expression match

Extract a regular expression match

Asked on January 11, 2019 in Regex.
Add Comment


  • 2 Answer(s)

                 To extract a regular expression match use new stringr package which wraps all the existing regular expression operates in a consistent syntax and adds a few that are missing:

    library(stringr)
    str_locate("aaa12xxx", "[0-9]+")
    #   start end
    # [1,]   4 5
    str_extract("aaa12xxx", "[0-9]+")
    # [1] "12"
    
    Answered on January 11, 2019.
    Add Comment

    Alternately try this to extract a regular expression match:

    gsub("[^0-9]", "", "aaa12xxxx")
    # [1] "12"
    
    Answered on January 11, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.