Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 - Fatal编程技术网

R 使用正则表达式查找字符'';和'';一串

R 使用正则表达式查找字符'';和'';一串,r,regex,R,Regex,我正在做一个正则表达式,只提取每个段落的第一句话。目前,我有一个如下的输入向量: text_insert <- c("hello, i am working through an r workbook. I am doing a regex expression.", "hi, how are you? I am great working through r") 但是,这无法识别?或作为句子的结尾 关于如何识别的任何帮助和?作为句末?您可以使用正则表达式: (\\.|!|?).* 或

我正在做一个正则表达式,只提取每个段落的第一句话。目前,我有一个如下的输入向量:

text_insert <- c("hello, i am working through an r workbook. I am doing a regex expression.", "hi, how are you? I am great working through r")
但是,这无法识别
作为句子的结尾

关于如何识别
的任何帮助
作为句末?

您可以使用正则表达式:

(\\.|!|?).*
或者,您可以查找“字符类中的任何一个符号”:

[.!?].*
在字符类中时不需要转义


最后,
gsub
非常适合替换文本,但实际上您所做的是搜索文本。有更好的功能;只是在R基地,它们使用起来很不方便。但是,我们可以使用包(例如stringr)轻松查找匹配项

使用此方法意味着您可以更直接地描述正在搜索的内容:由标点符号完成的字符序列:

〉stringr::str_match(text_insert, '.*?[.!?]')
     [,1]
[1,] "hello, i am working through an r workbook."
[2,] "hi, how are you?"

请注意
*?
:。这意味着只要第一个
实例出现,匹配就会停止

根据OP,第一句话以
结尾。有点奇怪,但这是他从问题中提出的要求

/^([^?!]*)/
捕获第一句话,直到

说明:

/^    -- beginning of the string, to capture the first sentence.
[^?!]*  -- move till you find either ? or !. Note that ^ in character class represents negation , meaning [NOT ? or !]

这是关于

的演示,因为您的正则表达式既没有提到
也没有提到
,为什么您希望它能找到它们?从某种意义上说,您的尝试并不是针对这个问题的认真尝试。那么
gsub(pattern=“([\\.\\?\\!])*”,replacement=“\\1”,x=text\u insert)
?这是否回答了您的问题?在什么情况下句号不是句子的结尾?@Frazer Bayliss,你是说你在问题中发布的示例中的第一句话是直到
/^    -- beginning of the string, to capture the first sentence.
[^?!]*  -- move till you find either ? or !. Note that ^ in character class represents negation , meaning [NOT ? or !]