如何在Java中匹配括号内(嵌套)的字符串? [英] How to match string within parentheses (nested) in Java?

查看:25
本文介绍了如何在Java中匹配括号内(嵌套)的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想匹配括号内的字符串,例如:

I would like to match a string within parentheses like:

(i, j, k(1))
^^^^^^^^^^^^

字符串也可以包含闭括号.如何在不编写解析器的情况下将其与 Java 中的正则表达式匹配,因为这是我项目的一小部分.谢谢!

The string can contain closed parentheses too. How to match it with regular expression in Java without writing a parser, since this is a small part of my project. Thanks!

我想搜索一个字符串块并找到类似 u(i, j, k)u(i, j, k(1)) 或只需 u(<此配对括号内的任何内容>),并将它们替换为 __u%array(i, j, k)__u%array(i, j, k(1)) 用于我的 Fortran 翻译应用程序.

I want to search out a string block and find something like u(i, j, k), u(i, j, k(1)) or just u(<anything within this paired parens>), and replace them to __u%array(i, j, k) and __u%array(i, j, k(1)) for my Fortran translating application.

推荐答案

正如我所说,与流行的看法(不要相信人们所说的一切)相反,使用正则表达式匹配嵌套括号是可能的.

As I said, contrary to popular belief (don't believe everything people say) matching nested brackets is possible with regex.

使用它的缺点是您只能嵌套到固定级别.对于您希望支持的每个额外级别,您的正则表达式将越来越大.

The downside of using it is that you can only up to a fixed level of nesting. And for every additional level you wish to support, your regex will be bigger and bigger.

但是不要相信我的话.让我演示给你看.正则表达式:

But don't take my word for it. Let me show you. The regex:

([^()]*)

匹配一级.对于最多两个级别,您需要:

(([^()]*|([^()]*))*)

等等.要继续添加级别,您只需将中间(第二个)[^()]* 部分更改为 ([^()]*|([^()]*))*(在此处检查三个级别).正如我所说,它会越来越大.

And so on. To keep adding levels, all you have to do is change the middle (second) [^()]* part to ([^()]*|([^()]*))* (check three levels here). As I said, it will get bigger and bigger.

对于您的情况,两个级别可能就足够了.所以它的 Java 代码是:

For your case, two levels may be enough. So the Java code for it would be:

String fortranCode = "code code u(i, j, k) code code code code u(i, j, k(1)) code code code u(i, j, k(m(2))) should match this last 'u', but it doesnt.";
String regex = "(\w+)(\(([^()]*|\([^()]*\))*\))"; // (w+)((([^()]*|([^()]*))*))
System.out.println(fortranCode.replaceAll(regex, "__$1%array$2"));

输入:

code code u(i, j, k) code code code code u(i, j, k(1)) code code code u(i, j, k(m(2))) should match this last 'u', but it doesnt.

输出:

code code __u%array(i, j, k) code code code code __u%array(i, j, k(1)) code code code u(i, j, __k%array(m(2))) should match this last 'u', but it doesnt.

底线:

在一般情况下,解析器会做得更好 - 这就是人们对它如此生气的原因.但对于简单的应用程序,正则表达式几乎就足够了.

Bottom line:

In the general case, parsers will do a better job - that's why people get so pissy about it. But for simple applications, regexes can pretty much be enough.

注意:某些正则表达式支持嵌套运算符 R(Java 不支持,PHP 和 Perl 等 PCRE 引擎支持),它允许您嵌套任意数量的级别.有了它们,你可以这样做:(([^()]|(?R))*).

Note: Some flavors of regex support the nesting operator R (Java doesn't, PCRE engines like PHP and Perl do), which allows you to nest arbitrary number of levels. With them, you could do: (([^()]|(?R))*).

这篇关于如何在Java中匹配括号内(嵌套)的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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