R中两个字符之间可变长度单词的正则表达式
regular expression in R for word of variable length between two characters
如何从下面的字符串中提取单词 wordofvariablelength。
<a href=\"http://www.adrive.com/browse/wordofvariablelength\" class=\"next-button\" id=\"explore-gutter\" data-linkid=\"huiazc\"> <strong class=\"text gutter-text \">
我能够使用下面的代码获取字符串的第一部分,但是是否有一个正则表达式我可以用来只获取 "browse/" 之后和“\”之前的单词,在这里是单词,"wordofvariablelength" 使用下面的代码
mystring = substr(mystring,nchar("<a href=\"http://www.thesaurus.com/browse/")+1,nchar("<a href=\"http://www.thesaurus.com/browse/")+20)
请注意,单词 wordofvariablelength 可以是任意长度,因此我无法对开始和结束进行硬编码
尝试
sub('.*?\.com/[^/]*\/([a-z]+).*', '\1', mystring)
#[1] "wordofvariablelength"
或者
library(stringr)
str_extract(mystring, perl('(?<=browse/)[A-Za-z]+'))
#[1] "wordofvariablelength"
数据
mystring <- "<a href=\"http://www.adrive.com/browse/wordofvariablelength\" class=\"next-button\" id=\"explore-gutter\" data-linkid=\"huiazc\"> <strong class=\"text gutter-text \">"
通过regmatches函数。
> x <- "<a href=\"http://www.adrive.com/browse/wordofvariablelength\" class=\"next-button\" id=\"explore-gutter\" data-linkid=\"huiazc\"> <strong class=\"text gutter-text \">"
> regmatches(x, regexpr('.*?"[^"]*/\K[^/"]*(?=")', x, perl=TRUE))
[1] "wordofvariablelength"
或
> regmatches(x, regexpr('[^/"]*(?="\s+class=")', x, perl=TRUE))
[1] "wordofvariablelength"
或
使用 gsub 更简单。
> gsub('.*/|".*', "", x)
[1] "wordofvariablelength"
你可以使用这个正则表达式
/browse\/(.*?)\/g
您可以使用以下正则表达式 (?<=browse/).*?(?=\")
。
正则表达式的意思是:检查我们是否有 browse/
,然后取出所有后续字符(但不消耗)\
.
示例代码(和一个sample program here):
mystr <- "<a href=\"http://www.adrive.com/browse/wordofvariablelength\" class=\"next-button\" id=\"explore-gutter\" data-linkid=\"huiazc\"> <strong class=\"text gutter-text \">"
regmatches(mystr, regexpr('(?<=browse/).*?(?=\")', mystr, perl=T))
perl=T
意味着我们正在使用 Perl-like regex flavor,允许使用固定宽度的后视((?<=browse/)
)。
输出:
[1] "wordofvariablelength"
如何从下面的字符串中提取单词 wordofvariablelength。
<a href=\"http://www.adrive.com/browse/wordofvariablelength\" class=\"next-button\" id=\"explore-gutter\" data-linkid=\"huiazc\"> <strong class=\"text gutter-text \">
我能够使用下面的代码获取字符串的第一部分,但是是否有一个正则表达式我可以用来只获取 "browse/" 之后和“\”之前的单词,在这里是单词,"wordofvariablelength" 使用下面的代码
mystring = substr(mystring,nchar("<a href=\"http://www.thesaurus.com/browse/")+1,nchar("<a href=\"http://www.thesaurus.com/browse/")+20)
请注意,单词 wordofvariablelength 可以是任意长度,因此我无法对开始和结束进行硬编码
尝试
sub('.*?\.com/[^/]*\/([a-z]+).*', '\1', mystring)
#[1] "wordofvariablelength"
或者
library(stringr)
str_extract(mystring, perl('(?<=browse/)[A-Za-z]+'))
#[1] "wordofvariablelength"
数据
mystring <- "<a href=\"http://www.adrive.com/browse/wordofvariablelength\" class=\"next-button\" id=\"explore-gutter\" data-linkid=\"huiazc\"> <strong class=\"text gutter-text \">"
通过regmatches函数。
> x <- "<a href=\"http://www.adrive.com/browse/wordofvariablelength\" class=\"next-button\" id=\"explore-gutter\" data-linkid=\"huiazc\"> <strong class=\"text gutter-text \">"
> regmatches(x, regexpr('.*?"[^"]*/\K[^/"]*(?=")', x, perl=TRUE))
[1] "wordofvariablelength"
或
> regmatches(x, regexpr('[^/"]*(?="\s+class=")', x, perl=TRUE))
[1] "wordofvariablelength"
或
使用 gsub 更简单。
> gsub('.*/|".*', "", x)
[1] "wordofvariablelength"
你可以使用这个正则表达式
/browse\/(.*?)\/g
您可以使用以下正则表达式 (?<=browse/).*?(?=\")
。
正则表达式的意思是:检查我们是否有 browse/
,然后取出所有后续字符(但不消耗)\
.
示例代码(和一个sample program here):
mystr <- "<a href=\"http://www.adrive.com/browse/wordofvariablelength\" class=\"next-button\" id=\"explore-gutter\" data-linkid=\"huiazc\"> <strong class=\"text gutter-text \">"
regmatches(mystr, regexpr('(?<=browse/).*?(?=\")', mystr, perl=T))
perl=T
意味着我们正在使用 Perl-like regex flavor,允许使用固定宽度的后视((?<=browse/)
)。
输出:
[1] "wordofvariablelength"