带 stringr 的正则表达式::如何查找模式的第一个实例

标签 regex r latex knitr stringr

这个问题的背后是提取由 knitrlatex 创建的所有引用的努力。没有找到其他方法,我的想法是读入 R 的 .Rnw 脚本并使用正则表达式来查找引用 - 其中 latex 语法是 \ref{caption referenced to}。我的脚本有 250 多个引用文献,其中一些彼此非常接近。

下面的 text.1 示例有效,但 text 示例无效。我认为这与 R 一直到最后的右大括号有关。如何在第一个右大括号处停止并将其前面的内容提取到左大括号?

library(stringr)
text.1 <- c(" \\ref{test}", "abc", "\\ref{test2}", " \\section{test3}", "{test3")
# In the regular expression below, look back and if find "ref{", grab everything until look behind for } at end
# braces are special characters and require escaping with double backslacs for R to recognize them as braces
# unlist converts the list returned by str_extract to a vector

unlist(str_extract_all(string = text.1, pattern = "(?<=ref\\{).*(?=\\}$)"))
[1] "test"  "test2"

# a more complicated string, with more than one set of braces in an element
text <- c("text \ref{?bar labels precision} and more text  \ref{?table column alignment}", "text \ref{?table space} }")

unlist(str_extract_all(string = text, pattern = "(?<=ref\\{).*(?=\\}$)"))
character(0)

最佳答案

text 的问题是“ref”前面的反斜杠被引擎和 R 的解析器解释为回车符 \r;所以你试图匹配“ref”,但它实际上是 (CR + "ef") ...

此外,* 默认情况下是贪婪,这意味着它将尽可能匹配,并且仍然允许正则表达式的其余部分匹配。使用 *? 或否定字符类来防止贪婪。

unlist(str_extract_all(text, '(?<=\ref\\{)[^}]*'))
# [1] "?bar labels precision"   "?table column alignment" "?table space"

如您所见,您可以使用字符类来匹配 (\rr + "ef") .. .

x <- c(' \\ref{test}', 'abc', '\\ref{test2}', ' \\section{test3}', '{test3',
       'text \ref{?bar labels precision} and more text  \ref{?table column alignment}', 
       'text \ref{?table space} }')

unlist(str_extract_all(x, '(?<=[\rr]ef\\{)[^}]*'))

# [1] "test"                    "test2"                   "?bar labels precision"  
# [4] "?table column alignment" "?table space" 

关于带 stringr 的正则表达式::如何查找模式的第一个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32811800/

相关文章:

javascript - [Javascript]正则表达式

python-3.x - Python正则表达式查找双引号中的值(忽略其他语言字符)

python - 获取全括号表达式的所有部分

r - 如何根据R中缺失数据的日期列计算数据框中多列的月平均值

r - 如何使用 dplyr 创建总频率表

matlab - 我如何让我的 makefile 运行 matlab 脚本

Java正则表达式查找所有介于但最后一个字符之前或之后的字符

使用 XLConnect 读取包含缺失值的 .xls 文件

标题中的 MatLab sqrt 符号

python - 用于匹配右大括号的正则表达式