2017-06-18 90 views
0

谁能解释如何图案被发现在这里grepl和GSUB是罚款了here.But模式不容易理解不清楚正则表达式模式

if (grepl("\\(.*?\\)", name)){  
    gsub("^.*?\\((.*?)\\)$", "\\1", name) 
}  
+0

它们使用[regular exp (https://en.wikipedia.org/wiki/Regular_expression),除非你设置参数fixed = TRUE,在这种情况下,他们只是在字符向量内搜索模式字符串(因为它是)。 – digEmAll

回答

0

\\(* \\?):

\\ -> \ 
    (-> (
    . -> . 
    * -> zero ore more times the last character 
     ? -> last item is optional (not needed here) 
     \\ -> \ 
     ) ->) 

所以这得到所有字符串像\(\)\(.\)\(....\)

^。 ?\\(?()\\)$:

^.*?\\((.*?)\\)$ 
^ -> Beginning of line 
. -> . 
    * -> zero or more times last item 
    ? -> optional last item (not needed here) 
    \\ -> \ 
     ((. -> ((. 
      * -> zero or more times 
      ? -> optional last item 
      )\\ ->)\ 
       $ -> End of line 

请参阅R documentation正则表达式。

0

检查出regex101为您的模式的详细说明。

"字符"字面上(区分大小写) \\字符\字面上匹配(区分大小写) 第一捕获组(.*?\\) .*?匹配的任何字符(除了行终止) *?量词匹配 - 零和无限之间的匹配次数越少越好(懒惰) \\字符匹配\(区分大小写)