正则表达式 - 字符类中的括号和括号 [英] regex - brackets and parenthesis in character-class

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

问题描述

我想在正则表达式的字符类中匹配这些字符:[] (),我该怎么做?

I would like to match these characters: [ ] ( ) in a character class in a regex, how can I do that?

echo 'some text (some text in parenthesis) [some other in brackets]' | grep '[[]()]'

这个不匹配任何字符.

推荐答案

你可以这样使用:

echo 'some text (some text in paranthesis) [some other in brackets]' | grep -o '[][()]'

(
)
[
]

你不需要在字符类中转义 () .此外,如果您在打开 [ 后立即放置 ][ ,那么您也不需要转义它们.

You don't need to escape ( and ) inside a character class. Moreover if you place ] and [ right after opening [ then you don't need to escape them either.

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

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