7

Here is a string like this in Java.

String string="abc$[A]$def$[B]$ghi";

I want to search words that are located in $[*]$ pattern. The result of above the string is A, B.

1
  • 1
    Regex is any good to you? Commented Sep 28, 2012 at 8:13

3 Answers 3

7
    String s = "abc$[A]$def$[B]$ghi";
    Pattern p = Pattern.compile("\\$\\[.*?\\]\\$");
    Matcher m = p.matcher(s);
    while(m.find()){
        String b =  m.group();
        System.out.println(">> " +b.substring(2, b.length()-2));
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Use a regular expression. In Java, you can use the Pattern class.

Comments

0

You could use regular expressions for that. Take a look at the classes Pattern and Matcher.

The regular expression you would use in that case would be:

\$\[.*?\]\$

Alternatively, you could work with String.indexOf and String.substr.

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.