R regular expressions: unexpected behavior of “[:digit:]”

前端 未结 3 2011
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-15 04:03

I\'d like to extract elements beginning with digits from a character vector but there\'s something about POSIX regular expression syntax that I don\'t understand.

I

相关标签:
3条回答
  • 2020-12-15 04:18

    Try

    grep(pattern="[[:digit:]]", x=vec)
    

    instead as the 'meta-patterns' between colons usually require double brackets.

    0 讨论(0)
  • 2020-12-15 04:24
    man 7 regex
    

    Within a bracket expression, the name of a character class enclosed in "[:" and ":]" stands for the list of all characters belonging to that class. Standard character class names are:

             alnum       digit       punct
             alpha       graph       space
             blank       lower       upper
             cntrl       print       xdigit
    

    Therefore a character class that is the sole member of a bracket expression will look like double-brackets, such as [[:digit:]]. As another example, consider that [[:alnum:]] is equivalent to [[:alpha:][:digit:]].

    0 讨论(0)
  • 2020-12-15 04:26

    Another solution

    grep(pattern="\\d", x=vec)
    
    0 讨论(0)
提交回复
热议问题