Partial string matching using patterns

后端 未结 2 1799
太阳男子
太阳男子 2021-01-13 15:58

I need to write a query in R to match partial string in column names. I am looking for something similar to LIKE operator in SQL. For e.g, if I know beginning, middle or end

相关标签:
2条回答
  • 2021-01-13 16:37

    In regular expressions, ^ specifies the beginning of the string, $ specifies the end, thus:

    y<- c("I am looking for a dog", "looking for a new dog", "a dog", "I am just looking")
    grep("^looking.*dog$", y)
    [1] 2
    
    0 讨论(0)
  • 2021-01-13 16:44

    The grep function supports regular expressions and with regular expressions, you can match almost anything

    y<- c("I am looking for a dog", "looking for a new dog", "a dog", "I am just looking")
    grep("looking.*dog",y, value=T)
    # [1] "I am looking for a dog" "looking for a new dog" 
    

    Here this pattern looks for looking then "maybe something" then dog. So that should do what you want.

    0 讨论(0)
提交回复
热议问题