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

查看:711
本文介绍了如何匹配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(<此配对的parens中的任何内容>),并将它们替换为 __ u%array(i,j,k) __ u%array(i,j,k(1))

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