I need to use regex for following text. The parenthesis are not nested.
{ ... 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 ?
EDIT : The actually parenthesis are @{
and }@~enc~
Actual string:
@{ ... bla ... @{text}@~enc~ jdjdj ... @{ kdkdkdkd aaa @{ abc }@~enc~
You need to use a negated character class [^{}]
:
\{([^{}]*)}
The [^{}]
character class matches any character but {
and }
.
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.
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 }
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.