Java正则表达式匹配方括号 [英] Java Regular expression matching square brackets

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

问题描述

我正在尝试使用正则表达式(java replaceAll)执行以下操作:

I'm trying to do the following using regular expression (java replaceAll):

**Input:**
Test[Test1][Test2]Test3

**Output**
TestTest3

简而言之,我需要删除方括号内的所有内容,包括方括号。

In short, i need to remove everything inside square brackets including square brackets.

我正在尝试这个,但它不起作用:

I'm trying this, but it doesn't work:

\\[(.*?)\\]

你能帮忙吗?

谢谢,
腰带

推荐答案

你可以试试这个正则表达式:

You can try this regex:

\[[^\[]*\]

并替换为空

演示

示例Java源:

final String regex = "\\[[^\\[]*\\]";
final String string = "Test[Test1][Test2]Test3\n";
final String subst = "";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
final String result = matcher.replaceAll(subst);
System.out.println(result);

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

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