匹配所有带有标点符号的元素(r中的星号除外)

我有一个向量vec,其中包含带有标点符号的元素。我想返回所有带有标点符号的元素,除了带有星号的元素。

vec <- c("a,","abc","ef","abc-","abc|","abc*01")
> vec[grepl("[^*][[:punct:]]", vec)]
[1] "a,"     "abc-"   "abc|"   "abc*01"

如果有否定标记[^ *],为什么它返回“ abc * 01”?

评论
  • 闹心*
    闹心* 回复

    You could use grepl here:

    vec <- c("a,","abc-","abc|","abc*01")
    vec[grepl("^(?!.*\\*).*[[:punct:]].*$", vec, perl=TRUE)]
    
    [1] "a,"   "abc-" "abc|"
    

    The regex pattern used ^(?!.*\\*).*[[:punct:]].*$ will only match contents which does not contain any asterisk characters, while also containing at least one punctuation character:

    ^                from the start of the string
        (?!.*\*)     assert that no * occurs anywhere in the string
        .*           match any content
        [[:punct:]]  match any single punctuation character (but not *)
        .*           match any content
    $                end of the string