Regex named groups in R

后端 未结 1 1330
借酒劲吻你
借酒劲吻你 2021-01-18 21:52

For all intents and purposes, I am a Python user and use the Pandas library on a daily basis. The named capture groups in regex is extremely useful. So, for example, it is r

相关标签:
1条回答
  • 2021-01-18 22:39

    Base R does capture the information about the names but it doesn't have a good helper to extract them by name. I write a wrapper to help called regcapturedmatches. You can use it with

    myRegex = "(?<textOcc>t[e]?xt)|(?<wordOcc>word)"
    m<-regexpr(myRegex, origText, perl=T, ignore.case=T)
    regcapturedmatches(origText,m)
    

    Which returns

         textOcc wordOcc
    [1,] "text"  ""     
    [2,] "TEXT"  ""     
    [3,] "TXT"   ""     
    [4,] ""      "Word" 
    [5,] ""      ""     
    [6,] "text"  ""     
    [7,] ""      "word" 
    
    0 讨论(0)
提交回复
热议问题