Reputation: 359
I need to match entries like {@x anything}
in strings like a b c 1 225 {@x anything here1} test test {@x blabla} xyz test {@x any characters here}
. I tried \{(@x ([^\}].*\w+(\.*)\s*)*)\}
so far, but this is not really what I want and I am kinda stuck :(
So that should get:
anything here1
blabla
any characters here
Upvotes: 2
Views: 224
Reputation: 10549
It seems to me that you want to tell in pattern "match the minimum between characters '{' and '}'" so the pattern you can use is:
final String string = "a b c 1 225 {@x anything = here1} test test {@x bl** #abla} xyz test {@x any characters here}";
final Pattern pattern = Pattern.compile( "\\{@x (.*?)\\}" ); // <-- pattern
final Matcher matcher = pattern.matcher( string );
while ( matcher.find() )
System.out.println( matcher.group( 1 ) );
?
in .*?
is doing exactly that.
Upvotes: 1
Reputation: 20783
Another try:
String input="a b c 1 225 {@x anything here1} test test {@x blabla} xyz test {@x any characters here}";
String pattern = "\\{@x [(\\w*)(\\s*)]*\\}";
for( String s: input.split(pattern)){
System.out.println(s);
}
\w* = any word ( a-z,A-Z,0-9) ; *= 0 or more
\s* = white space ; *= 0 or more
[]* - repeating group.
Upvotes: 1
Reputation: 4446
This should do it:
String string = "a b c 1 225 {@x anything here1} test test {@x blabla} xyz test {@x any characters here}";
String regexp = "\\{\\@x ([^\\}]*)\\}";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(string);
while (matcher.find()){
System.out.println(matcher.group(1));
}
\{\@x - Matchers you start.
([^\}]*) - matches anyting except the end-curly (}) and puts that in a group (1)
\} - matches the end curly
Then you search and subtract your group.
Upvotes: 1
Reputation: 2050
Try this one: "({@x ([^{]*)})"
String string = "a b c 1 225 {@x anything = here1} test test {@x bl** #abla} xyz test {@x any characters here}";
String regexp = "(\\{\\@x ([^\\{]*)\\})";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(string);
while (matcher.find()){
System.out.println(matcher.group(2));
}
Upvotes: 1
Reputation: 5647
Well to extract all groups that come with that structure you can start with:
{@x [a-zA-Z0-9 ]+}
From this point just remove the header and end of the requested strings and you should have the required output.
EDIT:
I've updated the regex a bit:
{@x [\w= ]+}
Upvotes: 2