带括号的 R grep 模式正则表达式 [英] R grep pattern regex with brackets

查看:14
本文介绍了带括号的 R grep 模式正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中使用 grep 时遇到问题:

I have a problem with grep in R:

patterns= c("AB_(1)","AB_(2)")
text= c("AB_(1)","DDD","CC")

grep(patterns[1],text)
>integer(0)  ????

grep 命令的()"括号有问题,有没有我可以使用的 as.XX(patterns[1]) ??

the grep command has problem with "()" brackets, is there any as.XX(patterns[1]) that I can use??

推荐答案

你需要用双反斜杠转义:

You need escape by double backslash:

> patterns= c("AB_\\(1\\)","AB_(2)")
> text= c("AB_(1)","DDD","CC")
> 
> grep(patterns[1],text)
[1] 1

这篇关于带括号的 R grep 模式正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆