R 在匹配前找出两个单词

R 在匹配前找出两个单词,r,regex,stringr,R,Regex,Stringr,我正在尝试使用正则表达式拆分字符串。我的正则表达式代码应该匹配冒号前面的两个单词,最终目标是拆分如下内容: "Joe Biden: We need to reform healthcare. It is important. Bernie Sanders: I agree. It is important." "Joe Biden" "We need to reform healthcare. It is important." "Bernie Sanders" "I agree. It is

我正在尝试使用正则表达式拆分字符串。我的正则表达式代码应该匹配冒号前面的两个单词,最终目标是拆分如下内容:

"Joe Biden: We need to reform healthcare. It is important. Bernie Sanders: I agree. It is important."
"Joe Biden" "We need to reform healthcare. It is important." "Bernie Sanders" "I agree. It is important"
foo <- strsplit(my_string, split="(?<=.)(?=(\\S+)\\s*(\\S+)\\s*:)",perl=TRUE)
转化为字符串向量,如下所示:

"Joe Biden: We need to reform healthcare. It is important. Bernie Sanders: I agree. It is important."
"Joe Biden" "We need to reform healthcare. It is important." "Bernie Sanders" "I agree. It is important"
foo <- strsplit(my_string, split="(?<=.)(?=(\\S+)\\s*(\\S+)\\s*:)",perl=TRUE)
我得到的最接近的结果是:

foo <- strsplit(my_string, split="(\\S+)\\s*(\\S+)\\s*:",perl=TRUE)


是否有替代的正则表达式代码来实现这一点,或者我应该使用不同的函数?

这将拆分由or运算符|分隔的两个对象。1一个空格后跟两个单词,两个单词之间用空格和冒号隔开,2一个冒号后跟空格

my_string <- "Joe Biden: We need to reform healthcare. It is important. Bernie Sanders: I agree. It is important."
strsplit(my_string, split="( (?=\\w+ \\w+:)|: )",perl=TRUE)
[[1]]
[1] "Joe Biden"            "We need to reform healthcare. It is important."
[3] "Bernie Sanders"       "I agree. It is important."    

你在这里遇到的麻烦是如果演讲者的名字只有一个词。这就是我在回答你上一个问题时寻找标点符号的目的

这将拆分由or运算符|分隔的两个对象。1一个空格后跟两个单词,两个单词之间用空格和冒号隔开,2一个冒号后跟空格

my_string <- "Joe Biden: We need to reform healthcare. It is important. Bernie Sanders: I agree. It is important."
strsplit(my_string, split="( (?=\\w+ \\w+:)|: )",perl=TRUE)
[[1]]
[1] "Joe Biden"            "We need to reform healthcare. It is important."
[3] "Bernie Sanders"       "I agree. It is important."    

你在这里遇到的麻烦是如果演讲者的名字只有一个词。这就是我在回答你上一个问题时寻找标点符号的目的

你想达到什么不同于你的目标?最后一个答案非常有用,但前提是语句以标点符号结尾。对于某些陈述,主持人会在句子中间打断演讲者,这样就没有标点符号了。在冒号演讲者的名字之前匹配两个单词将涵盖所有情况。您试图实现与您的名字不同的目标是什么?最后一个答案非常有用,但前提是语句以标点符号结尾。对于某些陈述,主持人会在句子中间打断演讲者,这样就没有标点符号了。匹配冒号演讲者姓名前的两个单词将涵盖所有情况。