R相当于MATLAB';s';代币';regexp中的选项

R相当于MATLAB';s';代币';regexp中的选项,regex,r,matlab,Regex,R,Matlab,我正在学习regexp,但没有找到与MATLAB的代币等效的R: hstr='Default'; expr='.''; [mat,tok]=regexp(hstr,expr,'match','tokens'); mat{:} ans= ans= 违约 tok{:} ans= “a” ans= “b” 在R中实现这一点的最佳方法是什么?您可以使用stringr库中的函数返回所有匹配项和子匹配项(=捕获的文本) 但请注意,它不支持PCRE正则表达式。在特定情况下,当您需要查找或跳过失败谓词时,可能

我正在学习regexp,但没有找到与MATLAB的
代币
等效的R:

hstr='Default
'; expr='.''; [mat,tok]=regexp(hstr,expr,'match','tokens'); mat{:} ans= ans= 违约 tok{:} ans= “a” ans= “b”
在R中实现这一点的最佳方法是什么?

您可以使用
stringr
库中的函数返回所有匹配项和子匹配项(=捕获的文本)


但请注意,它不支持PCRE正则表达式。在特定情况下,当您需要查找或跳过失败谓词时,可能会出现问题。

dedired输出是什么?(有些人没有MATLAB。)@lukeA我在MatlabR2015A中运行了这段代码,并添加了精确的输出。嗯,您得到了
regmatches(hstr,gregexpr('.'?',hstr))[[1]
regmatches(hstr,regexec('.'?',hstr)[[1]
。我不知道有一个函数将两者结合起来。(然而,这并不意味着没有——让我们看看。)
hstr = '<!comment><a name="752507"></a><b>Default</b><br>';
expr = '<(\w+).*?>.*?</\1>';

[mat,tok] = regexp(hstr, expr, 'match', 'tokens');

mat{:}
ans =
<a name="752507"></a>
ans =
<b>Default</b>

tok{:}
ans = 
    'a'
ans = 
    'b'
> library(stringr)
> hstr = '<!comment><a name="752507"></a><b>Default</b><br>'
> expr = '<(\\w+).*?>.*?</\\1>'
> results = str_match_all(hstr, expr)
> unlist(results[[1]][,2])
[1] "a" "b"
> results
[[1]]
     [,1]                      [,2]
[1,] "<a name=\"752507\"></a>" "a" 
[2,] "<b>Default</b>"          "b" 
> sapply(regmatches(hstr,gregexpr(expr,hstr))[[1]], function(m) unlist(regmatches(m,regexec(expr,m))))
     <a name="752507"></a>     <b>Default</b>  
[1,] "<a name=\"752507\"></a>" "<b>Default</b>"
[2,] "a"                       "b"