r grep by regex - finding a string that contains a sub string exactly one once

前端 未结 6 1891
暗喜
暗喜 2021-01-25 22:10

I am using R in Ubuntu, and trying to go over list of files, some of them i need and some of them i don\'t need,

I try to get the one\'s i need by finding a sub string

6条回答
  •  感情败类
    2021-01-25 22:39

    As I said in comments, grep looks for a pattern inside your string and there is indeed "a" (or "a{1}", which is the same for grep) in "aa". You need to add to the pattern that the "a" is followed by not a : "a[^a]":

    grep("a[^a]", c("aa", "ab"), value=TRUE)
    #[1] "ab"
    

    EDIT

    Considering your specific problem, it seems you can try by the "opposite" : filter out the strings that contains more than one occurence of the pattern, using a "capture" of the pattern:

    !grepl("(ab).+\\1", c("ab.t", "ab-ab.t"))
    #[1]  TRUE FALSE
    
    !grepl("(ab).*\\1", c("ab", "ab-ab","ab-cc-ab", "abab"))
    #[1]  TRUE FALSE FALSE FALSE
    

    The brackets permit to capture the pattern (here ab but it can be any regex), the .* is for "anything" zero or more times and the \\1 asks for a repeat of the captured pattern

提交回复
热议问题