字符串检测无法使用 str_detect() [重复]

问题描述 投票:0回答:1

我想知道为什么这段代码返回 TRUE :

str_detect("Average (10)","(0)")
TRUE

正确答案应该是 FALSE,那么我应该在 str_detect 函数中使用哪个正则表达式?

谢谢

r regex tidyverse stringr
1个回答
0
投票

str_detect("Average (10)","\\(0\\)")

左括号和右括号是正则表达式中的元字符,因此您需要使用反斜杠字符对其进行转义。由于反斜杠字符也是元字符,因此需要将其放入两次。

© www.soinside.com 2019 - 2024. All rights reserved.