Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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
Regex 当每个观察值的第一个字(相同的起始字符串模式)在R中包含不同的结束字符串时,如何分离连接的字符串?_Regex_R_String_String Concatenation - Fatal编程技术网

Regex 当每个观察值的第一个字(相同的起始字符串模式)在R中包含不同的结束字符串时,如何分离连接的字符串?

Regex 当每个观察值的第一个字(相同的起始字符串模式)在R中包含不同的结束字符串时,如何分离连接的字符串?,regex,r,string,string-concatenation,Regex,R,String,String Concatenation,在R中,我有一个更大的数据集,其中包含我需要解决的问题。因此,在R中有一个数据帧,Post变量中每个观察值的第一个字都有一个串联字符串。幸运的是,字符串的开头包含相同的单词,但连接字符串的结尾总是不同的。有谁知道有一个函数可以将引言从它连接(附加)到一串单词的单词中分离出来吗?换句话说,当每个观察值的第一个单词(Introduction“相同的开始字符串模式”)在R中包含不同的结束字符串时,如何分离连接的字符串 更新:完整且可重复的问题 dat <- data.frame(author=

在R中,我有一个更大的数据集,其中包含我需要解决的问题。因此,在R中有一个数据帧,
Post
变量中每个观察值的第一个字都有一个串联字符串。幸运的是,字符串的开头包含相同的单词,但连接字符串的结尾总是不同的。有谁知道有一个函数可以将
引言
从它连接(附加)到一串单词的单词中分离出来吗?换句话说,当每个观察值的第一个单词(
Introduction
“相同的开始字符串模式”)在R中包含不同的结束字符串时,如何分离连接的字符串

更新:完整且可重复的问题

 dat <- data.frame(author=c("a", "b", "c", "d", "a", "b", "c", "d", "e", "a", "a", "a","a", "a", "c","c","c","c"),Post=c("Introductiontwo text", "IntroductionYoua need Introduction to give a complete and reproducible questionone text", "IntroductionYouas need Introduction to give a complete and reproducible questionthre text", "IntroductionYouasd need Introduction to give a complete and reproducible questionnice text", "IntroductionYouasds need Introduction to give a complete and reproducible questionwow text", "IntroductionYouasdsh need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshs need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsas need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasa need Introduction to give a complete and reproducible questionone text","IntroductionYouasdshsasaa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text","IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text","IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text","IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text"))


dat

         author                                                                                              Post
1       a                                                                              Introductiontwo text
2       b           IntroductionYoua need Introduction to give a complete and reproducible questionone text
3       c         IntroductionYouas need Introduction to give a complete and reproducible questionthre text
4       d        IntroductionYouasd need Introduction to give a complete and reproducible questionnice text
5       a        IntroductionYouasds need Introduction to give a complete and reproducible questionwow text
6       b       IntroductionYouasdsh need Introduction to give a complete and reproducible questionone text
7       c      IntroductionYouasdshs need Introduction to give a complete and reproducible questionone text
8       d     IntroductionYouasdshsa need Introduction to give a complete and reproducible questionone text
9       e    IntroductionYouasdshsas need Introduction to give a complete and reproducible questionone text
10      a   IntroductionYouasdshsasa need Introduction to give a complete and reproducible questionone text
11      a  IntroductionYouasdshsasaa need Introduction to give a complete and reproducible questionone text
12      a IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
13      a IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
14      a IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
15      c IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
16      c IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
17      c IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
18      c IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text

dat您可以对捕获组使用
gsub

gsub("(Introduction)(.+)","\\1 \\2", dat$Post)

括号包含“引言”和以下字符。然后我们将它们替换为中间有空格的匹配值。

您可以将
gsub
用于捕获组

gsub("(Introduction)(.+)","\\1 \\2", dat$Post)

括号包含“引言”和以下字符。然后,我们用中间有空格的匹配值替换它们。

如果另一个
引言
位于同一观察中,会怎么样?通过添加额外的空白,这不会影响非连接的引入吗?这就是为什么我说我有一个更大的数据集,类似于我的示例。那么,如果出现这种情况,为什么不制作一个示例来演示呢!?真的,我不明白。@RobertDove你需要给出一个完整且可重复的问题。@TimBiegeleisen你的方法是什么?我在这个问题上被吓坏了。再说一次,我坐地铁的时候正在用手机。如果另一个
简介
位于同一个观察点怎么办?通过添加额外的空白,这不会影响非连接的引入吗?这就是为什么我说我有一个更大的数据集,类似于我的示例。那么,如果出现这种情况,为什么不制作一个示例来演示呢!?真的,我不明白。@RobertDove你需要给出一个完整且可重复的问题。@TimBiegeleisen你的方法是什么?我在这个问题上被吓坏了。我又一次坐地铁,一边用手机。