2

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

2
  • So, what's the problem? What have you tried so far? Commented Mar 19, 2012 at 15:29
  • 2
    I mentioned I tried .... so far Commented Mar 19, 2012 at 15:30

5 Answers 5

2

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= ]+}
Sign up to request clarification or add additional context in comments.

2 Comments

I might need to have special characters there, such as the equal (=) sign.
I think he says that he can have multiple special characters(not only the equal sign).
1

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));
    }

Comments

1

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.

Comments

1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.