regex - R中两个字符之间可变长度单词的正则表达式

标签 regex r gsub

如何从下面的字符串中提取单词 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 \">"

关于regex - R中两个字符之间可变长度单词的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29533289/

相关文章:

r - 笛卡尔积数据框

mysql - 使用 Regex 解析 Bash 脚本中的 ClamAV 日志以插入 MySQL

python - 为什么正则表达式的 "non-capturing"组不起作用?

r - ggplot2:如何绘制正交回归线?

r - ggplot2中的=“h”类型的代码

删除R中的括号

Ruby 未定义的方法小写

r - 将字符串的前两个字符移到字符串中的特定字符之后

python - 我如何使用正则表达式来匹配 “the end” 而不允许在那里换行?

regex - Perl - 正则表达式只提取逗号分隔的字符串