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

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

问题描述

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

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 '[\[\]\(\)]'

t $匹配任何字符。

This one doesn't match any character.

推荐答案

您可以像这样使用它:

You can use it like this:

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天全站免登陆