2017-07-26 7 views
1

Rで他の文字を否定する際に、正規表現を使っていくつかの文字列をマッチングしたいと思います。以下の例では、マッチさせたい文字列を除外したいと思います。以下の例は、Regular expression to match a line that doesn't contain a word?の答えを使用しています。他の文字と一致する文字列を無効にする

私の混乱は、私はこれをしようとすると、greplがエラーをスローしていることである:greplで

エラー(mypattern、MYSTRING): 無効な正規表現「ボードゲーム|(^((?!ゲーム))* $。 )」、理由 '無効な正規表現' 結果(すなわちT、T、F)所望str_detectリターンを使用して実行

mypattern <- "boardgames|(^((?!games).)*$)" 
mystring <- c("boardgames", "boardgames", "games") 

grepl(mypattern, mystring) 

注、私はgreplを使用したいです。

答えて

2

デフォルトのオプションはPerl-compatible regexps?regexp

The perl = TRUE argument to grep, regexpr, gregexpr, sub, gsub and strsplit switches to the PCRE library that implements regular expression pattern matching using the same syntax and semantics as Perl 5.x, with just a few differences.

によると

を使用している場合に必要ですperl = FALSE

grepl(mypattern, mystring, perl = TRUE) 
#[1] TRUE TRUE FALSE 

あるとして私たちはperl = TRUEを必要とします

関連する問題