Regex “提取”之后的文本&引用;
我有一根绳子Regex “提取”之后的文本&引用;,regex,r,pattern-matching,Regex,R,Pattern Matching,我有一根绳子 x <- "Name of the Student? Michael Sneider" 但无法提取名称。我认为这应该会有所帮助 substr(x, str_locate(x, "?")+1, nchar(x)) 试试这个: sub('.*\\?(.*)','\\1',x) str\u match在这种情况下更有用 str_match(x, ".*\\?\\s(.*)")[, 2] #[1] "Michael Sneider" x为了利用问题的松散措辞,我们可以走得太
x <- "Name of the Student? Michael Sneider"
但无法提取名称。我认为这应该会有所帮助
substr(x, str_locate(x, "?")+1, nchar(x))
试试这个:
sub('.*\\?(.*)','\\1',x)
str\u match
在这种情况下更有用
str_match(x, ".*\\?\\s(.*)")[, 2]
#[1] "Michael Sneider"
x为了利用问题的松散措辞,我们可以走得太远,使用自然语言处理从字符串中提取所有名称:
library(openNLP)
library(NLP)
# you'll also have to install the models with the next line, if you haven't already
# install.packages('openNLPmodels.en', repos = 'http://datacube.wu.ac.at/', type = 'source')
s <- as.String(x) # convert x to NLP package's String object
# make annotators
sent_token_annotator <- Maxent_Sent_Token_Annotator()
word_token_annotator <- Maxent_Word_Token_Annotator()
entity_annotator <- Maxent_Entity_Annotator()
# call sentence and word annotators
s_annotated <- annotate(s, list(sent_token_annotator, word_token_annotator))
# call entity annotator (which defaults to "person") and subset the string
s[entity_annotator(s, s_annotated)]
## Michael Sneider
库(openNLP)
图书馆(NLP)
#如果您还没有安装,您还必须在下一行中安装模型
#install.packages('openNLPmodels.en',repos='http://datacube.wu.ac.at/,类型='source')
s
x <- "Name of the Student? Michael Sneider"
sub(pattern = ".+?\\?" , x , replacement = '' )
library(openNLP)
library(NLP)
# you'll also have to install the models with the next line, if you haven't already
# install.packages('openNLPmodels.en', repos = 'http://datacube.wu.ac.at/', type = 'source')
s <- as.String(x) # convert x to NLP package's String object
# make annotators
sent_token_annotator <- Maxent_Sent_Token_Annotator()
word_token_annotator <- Maxent_Word_Token_Annotator()
entity_annotator <- Maxent_Entity_Annotator()
# call sentence and word annotators
s_annotated <- annotate(s, list(sent_token_annotator, word_token_annotator))
# call entity annotator (which defaults to "person") and subset the string
s[entity_annotator(s, s_annotated)]
## Michael Sneider