Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 提取模式正则表达式的两个引用_R_Regex_Stringr - Fatal编程技术网

R 提取模式正则表达式的两个引用

R 提取模式正则表达式的两个引用,r,regex,stringr,R,Regex,Stringr,我有一个输入向量,如下所示: input <- c("fdsfs iwantthis (1,1,1,1) fdsaaa iwantthisaswell (2,3,4,5)", "fdsfs thistoo (1,1,1,1)") 我成功地提取了开头括号前的每个单词。 我试着这样做只是为了得到第一个字: > gsub(".*?[[:space:]](.*?)[[:space:]]\\(.*", "\\1", input) [1] "iwantthis" "thistoo"

我有一个输入向量,如下所示:

input <- c("fdsfs iwantthis (1,1,1,1) fdsaaa   iwantthisaswell (2,3,4,5)", "fdsfs thistoo (1,1,1,1)")
我成功地提取了开头括号前的每个单词。 我试着这样做只是为了得到第一个字:

> gsub(".*?[[:space:]](.*?)[[:space:]]\\(.*", "\\1", input)
[1] "iwantthis" "thistoo"  
但我无法让它在多次出现时工作:

    > gsub(".*?[[:space:]](.*?)[[:space:]]\\(.*?[[:space:]](.*?)[[:space:]]\\(.*", "\\1 \\2", input)
[1] "iwantthis iwantthisaswell" "fdsfs thistoo (1,1,1,1)"  
我管理过的最接近的是:

library(stringr)
> str_extract_all(input, "(\\S*)\\s\\(")
[[1]]
[1] "iwantthis ("       "iwantthisaswell ("

[[2]]
[1] "thistoo ("
我确信我的正则表达式中缺少了一些东西(不太擅长),但是什么呢?

您可以使用

> sapply(str_extract_all(input, "\\S+(?=\\s*\\()"), paste, collapse=" ")
[1] "iwantthis iwantthisaswell" "thistoo"
看。
\\S+(?=\\S*\\()
将从
字符前面有0+空格)之前的文本中提取所有1+非空格块。
sapply
with
paste
将用空格(with
collapse=“
)连接找到的匹配项

图案细节

  • \S+
    -1个或多个非空白字符
  • (?=\s*\()
    )-一种积极的前瞻(
    (?=…)
    ),需要存在0+个空格字符(
    \s*
    ),然后在当前位置右侧立即出现一个
    \(
    )字符
您可以使用

> sapply(str_extract_all(input, "\\S+(?=\\s*\\()"), paste, collapse=" ")
[1] "iwantthis iwantthisaswell" "thistoo"
请参阅。
\\S+(?=\\S*\\()
将从
前面的文本中提取所有1+非空白块(
字符前面有0+空白。
使用
粘贴
将找到的匹配项与空格连接起来(使用
折叠=

图案细节

  • \S+
    -1个或多个非空白字符
  • (?=\s*\()
    )-一种积极的前瞻(
    (?=…)
    ),需要存在0+个空格字符(
    \s*
    ),然后在当前位置右侧立即出现一个
    \(
    )字符
这在R:

gsub('\\w.+? ([^\\s]+) \\(.+?\\)','\\1', input, perl=TRUE)
结果:

[1] "iwantthis iwantthisaswell" "thistoo" 
更新为适用于一般情况。例如,现在通过搜索其他匹配项之间的非空格来查找“i_wanttthisawell2”

使用其他建议的一般案例输入:

general_cases <- c("fdsfs iwantthis (1,1,1,1) fdsaaa   iwantthisaswell (2,3,4,5)", 
                   "fdsfs thistoo (1,1,1,1) ",
                   "GaGa iwant_this (1,1,1,1)", 
                   "lal2!@#$%^&*()_+a i_wantthisaswell2 (2,3,4,5)")
gsub('\\w.+? ([^\\s]+) \\(.+?\\)','\\1', general_cases, perl=TRUE)
这在R中起作用:

gsub('\\w.+? ([^\\s]+) \\(.+?\\)','\\1', input, perl=TRUE)
结果:

[1] "iwantthis iwantthisaswell" "thistoo" 
更新为适用于一般情况。例如,现在通过搜索其他匹配项之间的非空格来查找“i_wanttthisawell2”

使用其他建议的一般案例输入:

general_cases <- c("fdsfs iwantthis (1,1,1,1) fdsaaa   iwantthisaswell (2,3,4,5)", 
                   "fdsfs thistoo (1,1,1,1) ",
                   "GaGa iwant_this (1,1,1,1)", 
                   "lal2!@#$%^&*()_+a i_wantthisaswell2 (2,3,4,5)")
gsub('\\w.+? ([^\\s]+) \\(.+?\\)','\\1', general_cases, perl=TRUE)

这里有一个使用
base R

unlist(regmatches(input, gregexpr("\\w+(?= \\()", input, perl = TRUE)))
#[1] "iwantthis"       "iwantthisaswell" "thistoo"  

这里有一个使用
base R

unlist(regmatches(input, gregexpr("\\w+(?= \\()", input, perl = TRUE)))
#[1] "iwantthis"       "iwantthisaswell" "thistoo"  

这很有魅力!我可以问一下,在gsub场景中,您如何通过捕获组来实现这一点吗?@User2321请澄清您的目标:使用
gsub
的纯单一正则表达式解决方案,还是“允许”关于regex函数有一些常见的编程逻辑?你想只使用base R吗?你给出的解决方案非常完美。我想看看你如何用一个纯regex for gsub(主要是好奇)来解决这个问题。好吧,我会看看,因为类似于
库(qdapRegex)
->
rm\U white(gsub)((?:(\\S+\\S*\\()),“\\1”和
gsub(“^\\s+\\\$”、“\\1”、gsub((?:(\\s+)\\s*\\()”、“\\1”、input))
还应执行以下操作job@User2321使用
gsub
的单个正则表达式方法在这里不起作用,因为您需要用空格替换,空格将不可避免地出现在字符串的开头或结尾,因此您至少必须使用
trimws
。这很有魅力!请问您在g中如何处理此问题捕获组的子场景?@User2321请澄清您的目标:使用
gsub
的纯单一正则表达式解决方案,还是“允许”关于regex函数有一些常见的编程逻辑?你想只使用base R吗?你给出的解决方案非常完美。我想看看你如何用一个纯regex for gsub(主要是好奇)来解决这个问题。好吧,我会看看,因为类似于
库(qdapRegex)
->
rm\U white(gsub)((?:(\\S+\\S*\\()),“\\1”和
gsub(“^\\s+\\\$”、“\\1”、gsub((?:(\\s+)\\s*\\()”、“\\1”、input))
还应执行以下操作job@User2321使用
gsub
的单个正则表达式方法在这里不起作用,因为您需要用空格替换,空格不可避免地出现在字符串的开头或结尾,因此您至少必须使用
trimws
。一般情况下,它不能处理大量字符串,例如
iwant_这个(1,1,1,1)
我也想要这个(2,3,4,5)
等等。它不会工作(它不会删除它)。我没有发布我的
gsub
解决方案,并将它们保留在注释中是有原因的。对于这个问题,不能只调用
gsub
解决方案。虽然你可以使用
gsubfn
做些什么,但它看起来非常麻烦。@Wiktor看起来你链接的正则表达式测试使用了一个特定于PHP的pcre选项。但是OP用[r]标记和r代码示例发布了这个问题。如果在r会话中运行,我的解决方案可以工作(我运行的是3.4.4)。当您使用特定于R的regex在线测试仪时,上述解决方案也会起作用:regex101.com上的PCRE选项准确地显示了
gsub
perl=TRUE
参数一起使用的行为。PHP使用PCRE,而
gsub
在传递
perl=TRUE
时使用PCRE。您的“解决方案”只能“正常工作”对于发布的一个单句OP,但它在一般情况下不起作用。更新显示gsub可以在捕获所有非空格字符(包括下划线和数字)的一般情况下起作用。例如,可以捕获“i_want this well2”,您的答案仍然是非常错误的。在一般情况下,它不会与大量字符串一起工作,例如
iwant\u this(1,1,1,1)
我还想要2(2,3,4,5)
等。它不起作用(它不会删除它)。我没有发布我的
gsub
解决方案,并将它们保留在注释中是有原因的。对于这个问题,不能只调用
gsub
解决方案。虽然你可以使用
gsubfn
做些什么,但它看起来非常麻烦。@Wiktor看起来你链接的正则表达式测试使用了一个特定于PHP的pcre选项。但是OP po