按R中的特定模式将文本转换为数据帧

按R中的特定模式将文本转换为数据帧,r,R,我有这样的文字: text <- "Jeon Bo-ram (born March 22, 1986):She, better known mononymously as Boram, is a South Korean singer and actress. She is best known as a member of the South Korean girl group T-ara. Taylor Alison Swift (born December 13, 1989): Sh

我有这样的文字:

text <- "Jeon Bo-ram (born March 22, 1986):She, better known mononymously as Boram, is a South Korean singer and actress. She is best known as a member of the South Korean girl group T-ara. Taylor Alison Swift (born December 13, 1989): She is an American singer-songwriter. One of the leading contemporary recording artists, she is known for narrative songs about her personal life, which have received widespread media coverage. Nickolas Gene Carter (born January 28, 1980): He is an American musician and actor. He is best known as a member of the pop group the Backstreet Boys. As of 2015, Carter has released three solo albums, Now or Never, I'm Taking Off and All American during breaks between Backstreet Boys schedules, and a collaboration with Jordan Knight titled Nick & Knight. He has made occasional television appearances and starred in his own reality shows, House of Carters and I (Heart) Nick Carter. He gained fame in the mid 1990s and early 2000s as a teen idol. He is also the older brother of singer Aaron Carter and the late Leslie Carter. Clyde Jackson Browne (born October 9, 1948): He is an American singer-songwriter, and musician who has sold over 18 million albums in the United States. Coming to prominence in the 1970s, Browne has written and recorded songs such as These Days, The Pretender, Running on Empty, Lawyers in Love, Doctor My Eyes, Take It Easy, and  For a Rocker. In 2004, he was inducted into the Rock and Roll Hall of Fame in Cleveland, Ohio, and given an honorary doctorate of music by Occidental College in Los Angeles, California."
name                      born                         detail
Jeon Bo-ram               born March 22, 1986          She, better known mononymously as Boram, is a South Korean singer and actress. She is best known as a member of the South Korean girl group T-ara. Taylor Alison Swift (born December 13, 1989): She is an American singer-songwriter. One of the leading contemporary recording artists, she is known for narrative songs about her personal life, which have received widespread media coverage.
Taylor Alison Swift       born December 13, 1989       She is an American singer-songwriter. One of the leading contemporary recording artists, she is known for narrative songs about her personal life, which have received widespread media coverage.
Nickolas Gene Carter      born January 28, 1980        He is an American musician and actor. He is best known as a member of the pop group the Backstreet Boys. As of 2015, Carter has released three solo albums, Now or Never, I'm Taking Off and All American during breaks between Backstreet Boys schedules, and a collaboration with Jordan Knight titled Nick & Knight. He has made occasional television appearances and starred in his own reality shows, House of Carters and I (Heart) Nick Carter. He gained fame in the mid 1990s and early 2000s as a teen idol. He is also the older brother of singer Aaron Carter and the late Leslie Carter.
Clyde Jackson Browne      born October 9, 1948         He is an American singer-songwriter, and musician who has sold over 18 million albums in the United States. Coming to prominence in the 1970s, Browne has written and recorded songs such as These Days, The Pretender, Running on Empty, Lawyers in Love, Doctor My Eyes, Take It Easy, and For a Rocker. In 2004, he was inducted into the Rock and Roll Hall of Fame in Cleveland, Ohio, and given an honorary doctorate of music by Occidental College in Los Angeles, California.
我试图解决这个问题,但解决不了问题

cbind(do.call(rbind, strsplit(text, ":")), sub(".*[ ]", "", text))

正则表达式是一种方法,只要您保证模式不会改变,或者如果它们改变了,您就可以创建一个适应性更强的正则表达式

library(stringi)
library(purrr)

text <- "Jeon Bo-ram (born March 22, 1986):She, better known mononymously as Boram, is a South Korean singer and actress. She is best known as a member of the South Korean girl group T-ara. Taylor Alison Swift (born December 13, 1989): She is an American singer-songwriter. One of the leading contemporary recording artists, she is known for narrative songs about her personal life, which have received widespread media coverage. Nickolas Gene Carter (born January 28, 1980): He is an American musician and actor. He is best known as a member of the pop group the Backstreet Boys. As of 2015, Carter has released three solo albums, Now or Never, I'm Taking Off and All American during breaks between Backstreet Boys schedules, and a collaboration with Jordan Knight titled Nick & Knight. He has made occasional television appearances and starred in his own reality shows, House of Carters and I (Heart) Nick Carter. He gained fame in the mid 1990s and early 2000s as a teen idol. He is also the older brother of singer Aaron Carter and the late Leslie Carter. Clyde Jackson Browne (born October 9, 1948): He is an American singer-songwriter, and musician who has sold over 18 million albums in the United States. Coming to prominence in the 1970s, Browne has written and recorded songs such as These Days, The Pretender, Running on Empty, Lawyers in Love, Doctor My Eyes, Take It Easy, and  For a Rocker. In 2004, he was inducted into the Rock and Roll Hall of Fame in Cleveland, Ohio, and given an honorary doctorate of music by Occidental College in Los Angeles, California."
现在看起来像这样:

lines
## [1] "Jeon Bo-ram (born March 22, 1986):"    
## [2] "She, better known mononymously as Boram, is a South Korean singer and actress. She is best known as a member of the South Korean girl group T-ara."
## [3] "Taylor Alison Swift (born December 13, 1989):"
## [4] "She is an American singer-songwriter. One of the leading contemporary recording artists, she is known for narrative songs about her personal life, which have received widespread media coverage."
## [5] "Nickolas Gene Carter (born January 28, 1980):"
## [6] "He is an American musician and actor. He is best known as a member of the pop group the Backstreet Boys. As of 2015, Carter has released three solo albums, Now or Never, I'm Taking Off and All American during breaks between Backstreet Boys schedules, and a collaboration with Jordan Knight titled Nick & Knight. He has made occasional television appearances and starred in his own reality shows, House of Carters and I (Heart) Nick Carter. He gained fame in the mid 1990s and early 2000s as a teen idol. He is also the older brother of singer Aaron Carter and the late Leslie Carter."
## [7] "Clyde Jackson Browne (born October 9, 1948):"
## [8] "He is an American singer-songwriter, and musician who has sold over 18 million albums in the United States. Coming to prominence in the 1970s, Browne has written and recorded songs such as These Days, The Pretender, Running on Empty, Lawyers in Love, Doctor My Eyes, Take It Easy, and  For a Rocker. In 2004, he was inducted into the Rock and Roll Hall of Fame in Cleveland, Ohio, and given an honorary doctorate of music by Occidental College in Los Angeles, California."      
正则表达式查找名称和日期模式,并将其拆分到任一侧。这个“过分割”,但是<代码> DeCub()/代码>处理空白行。

现在我们有了name/dob和description的行对,可以使用“by 2”索引对它们进行迭代:

如果您正在处理一个可能导致数千行的巨大文本博客,那么使用
list()
vs
data\u frame()
(内部块的最后一部分)将更快,并消耗更少的临时内存


现在,有可能创建一个正则表达式来查找每个三元组并提取它们,但是我会让其他人展示他们的l33t正则表达式技能,如果他们如此倾向于创建所说的几乎不可读的beast。

正则表达式是一种方法,前提是你保证模式不会改变,或者如果他们改变,你可以制作一个适应性更强的正则表达式

library(stringi)
library(purrr)

text <- "Jeon Bo-ram (born March 22, 1986):She, better known mononymously as Boram, is a South Korean singer and actress. She is best known as a member of the South Korean girl group T-ara. Taylor Alison Swift (born December 13, 1989): She is an American singer-songwriter. One of the leading contemporary recording artists, she is known for narrative songs about her personal life, which have received widespread media coverage. Nickolas Gene Carter (born January 28, 1980): He is an American musician and actor. He is best known as a member of the pop group the Backstreet Boys. As of 2015, Carter has released three solo albums, Now or Never, I'm Taking Off and All American during breaks between Backstreet Boys schedules, and a collaboration with Jordan Knight titled Nick & Knight. He has made occasional television appearances and starred in his own reality shows, House of Carters and I (Heart) Nick Carter. He gained fame in the mid 1990s and early 2000s as a teen idol. He is also the older brother of singer Aaron Carter and the late Leslie Carter. Clyde Jackson Browne (born October 9, 1948): He is an American singer-songwriter, and musician who has sold over 18 million albums in the United States. Coming to prominence in the 1970s, Browne has written and recorded songs such as These Days, The Pretender, Running on Empty, Lawyers in Love, Doctor My Eyes, Take It Easy, and  For a Rocker. In 2004, he was inducted into the Rock and Roll Hall of Fame in Cleveland, Ohio, and given an honorary doctorate of music by Occidental College in Los Angeles, California."
现在看起来像这样:

lines
## [1] "Jeon Bo-ram (born March 22, 1986):"    
## [2] "She, better known mononymously as Boram, is a South Korean singer and actress. She is best known as a member of the South Korean girl group T-ara."
## [3] "Taylor Alison Swift (born December 13, 1989):"
## [4] "She is an American singer-songwriter. One of the leading contemporary recording artists, she is known for narrative songs about her personal life, which have received widespread media coverage."
## [5] "Nickolas Gene Carter (born January 28, 1980):"
## [6] "He is an American musician and actor. He is best known as a member of the pop group the Backstreet Boys. As of 2015, Carter has released three solo albums, Now or Never, I'm Taking Off and All American during breaks between Backstreet Boys schedules, and a collaboration with Jordan Knight titled Nick & Knight. He has made occasional television appearances and starred in his own reality shows, House of Carters and I (Heart) Nick Carter. He gained fame in the mid 1990s and early 2000s as a teen idol. He is also the older brother of singer Aaron Carter and the late Leslie Carter."
## [7] "Clyde Jackson Browne (born October 9, 1948):"
## [8] "He is an American singer-songwriter, and musician who has sold over 18 million albums in the United States. Coming to prominence in the 1970s, Browne has written and recorded songs such as These Days, The Pretender, Running on Empty, Lawyers in Love, Doctor My Eyes, Take It Easy, and  For a Rocker. In 2004, he was inducted into the Rock and Roll Hall of Fame in Cleveland, Ohio, and given an honorary doctorate of music by Occidental College in Los Angeles, California."      
正则表达式查找名称和日期模式,并将其拆分到任一侧。这个“过分割”,但是<代码> DeCub()/代码>处理空白行。

现在我们有了name/dob和description的行对,可以使用“by 2”索引对它们进行迭代:

如果您正在处理一个可能导致数千行的巨大文本博客,那么使用
list()
vs
data\u frame()
(内部块的最后一部分)将更快,并消耗更少的临时内存

现在,可能已经可以创建一个正则表达式来找到每个三元组并提取它们,但如果其他人如此倾向于创建所说的几乎不可读的野兽,我将让他们展示他们的l33t正则表达式技能。

使用正则表达式

A <- unlist(strsplit(text, "[(](?=b)|[)]:|(?<=\\.) (?=[^.]+?\\(b)", perl=TRUE))
B <- sapply(A, function(i) trimws(i, "both"))
as.data.frame(matrix(B, ncol=3, byrow=TRUE))
A带正则表达式

A <- unlist(strsplit(text, "[(](?=b)|[)]:|(?<=\\.) (?=[^.]+?\\(b)", perl=TRUE))
B <- sapply(A, function(i) trimws(i, "both"))
as.data.frame(matrix(B, ncol=3, byrow=TRUE))

A添加一些您已经采取但尚未实施的步骤会有所帮助,因为这通常不是代码编写服务。正则表达式<代码>(.*?\((born.*?\):(.*)
添加一些您已经采取但尚未实施的步骤会有所帮助,因为这通常不是代码编写服务。正则表达式<代码>(.*)\((born.*):(.*)
非常感谢您的回复。非常感谢您的回复。