在最后一个有效括号之间的文本的正则表达式。

时间:2022-09-13 00:19:16

I need to use regex for following text. The parenthesis are not nested.

我需要在后面的文本中使用regex。括号不是嵌套的。

{ ... bla ... {text} jdjdj ... { kdkdkdkd aaa { abc }

if I use \{(.*?)\} then there is two groups:

如果我使用\{(.*?)\},则有两组:

... bla ... {text
kdkdkdkd aaa { abc

But I need

但我需要

text
abc

Is it possible to write such Regex in Java ?

是否可以用Java编写这样的Regex ?

EDIT: The actually parenthesis are @{ and }@~enc~

编辑:实际的括号是@{和}@~enc~

Actual string:

实际的字符串:

@{ ... bla ... @{text}@~enc~ jdjdj ... @{ kdkdkdkd aaa @{ abc }@~enc~

1 个解决方案

#1


3  

Case 1. Single-character delimiters

You need to use a negated character class [^{}]:

你需要使用一个否定字符类[^ { }):

\{([^{}]*)}

The [^{}] character class matches any character but { and }.

(^ { }]类匹配任何字符,但{和}。

Java demo:

Java演示:

String s = "{ ... bla ... {text} jdjdj ... { kdkdkdkd aaa { abc }";
Pattern pattern = Pattern.compile("\\{([^{}]*)}");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
    System.out.println(matcher.group(1).trim()); 
} 

Note that you can use String#trim() to get rid of trailing/leading whitespace in your captures.

注意,您可以使用String#trim()消除捕获中的尾随/领先空格。

Case 2. Multi-character delimiters

To match text between multi-character delimiters, you can use a tempered greedy token:

要匹配多字符分隔符之间的文本,可以使用调和贪婪令牌:

Pattern pattern = Pattern.compile("(?s)@\\{((?:(?!@\\{|}@~enc~).)*)}@~enc~");
                                            ^^^^^^^^^^^^^^^^^^^^^^

See another demo. The (?s) modifier (equal to setting the Pattern.DOTALL flag) makes a . match newlines, too.

看到另一个演示。修饰符(等于设置模式)。多高旗)做一个。匹配换行。

NB: Do not forget to escape the { symbol in Java regex, and you do not have to escape }.

NB:不要忘记在Java regex中转义{符号,而且不必转义}。

#1


3  

Case 1. Single-character delimiters

You need to use a negated character class [^{}]:

你需要使用一个否定字符类[^ { }):

\{([^{}]*)}

The [^{}] character class matches any character but { and }.

(^ { }]类匹配任何字符,但{和}。

Java demo:

Java演示:

String s = "{ ... bla ... {text} jdjdj ... { kdkdkdkd aaa { abc }";
Pattern pattern = Pattern.compile("\\{([^{}]*)}");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
    System.out.println(matcher.group(1).trim()); 
} 

Note that you can use String#trim() to get rid of trailing/leading whitespace in your captures.

注意,您可以使用String#trim()消除捕获中的尾随/领先空格。

Case 2. Multi-character delimiters

To match text between multi-character delimiters, you can use a tempered greedy token:

要匹配多字符分隔符之间的文本,可以使用调和贪婪令牌:

Pattern pattern = Pattern.compile("(?s)@\\{((?:(?!@\\{|}@~enc~).)*)}@~enc~");
                                            ^^^^^^^^^^^^^^^^^^^^^^

See another demo. The (?s) modifier (equal to setting the Pattern.DOTALL flag) makes a . match newlines, too.

看到另一个演示。修饰符(等于设置模式)。多高旗)做一个。匹配换行。

NB: Do not forget to escape the { symbol in Java regex, and you do not have to escape }.

NB:不要忘记在Java regex中转义{符号,而且不必转义}。