匹配括号的正则表达式 [英] Regular Expression for matching parentheses

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

问题描述

匹配'('在字符串中的正则表达式是什么?

What is the regular expression for matching '(' in a string?

以下是场景:

我有一个字符串

str = "abc(efg)";

我想将字符串拆分为'('使用正则表达式。对于我正在使用

I want to split the string at '(' using regular expression.For that i am using

Arrays.asList(Pattern.compile("/(").split(str))

但我收到以下异常。

java.util.regex.PatternSyntaxException: Unclosed group near index 2
/(

转义'('似乎不起作用。

推荐答案

两个选项:

首先,你可以使用 back 斜杠来逃避它 - \(

Firstly, you can escape it using a backslash -- \(

或者,因为它是一个单个字符,你可以将它放在一个不需要转义的字符类中 - [(]

Alternatively, since it's a single character, you can put it in a character class, where it doesn't need to be escaped -- [(]

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

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