3
\$\begingroup\$

I want to extract some string surrounded by certain patterns.

For Instance, the original String something like 1234@{78}dasdh@{1}fdsfs@{fdf}ad@{ and I want extract 78, 1, fdf from it.

Below is my code for the purpose

public class Test {

    // want to get "78", "1", "fdf"
    private static String targetStr = "1234@{78}dasdh@{1}fdsfs@{fdf}ad@{";

    public static void main (String[] args) throws Exception {

        List<String> parsed = new ArrayList<>();

        Pattern open = Pattern.compile("@\\{");
        Matcher oMatcher = open.matcher(targetStr);

        Pattern close = Pattern.compile("}");
        Matcher cMatcher = close.matcher(targetStr);

        while(oMatcher.find()) {
            if(cMatcher.find())  parsed.add(targetStr.substring(oMatcher.start() + 2, cMatcher.start()));
        }
        System.out.println(parsed);
    }
}

Is there anyway to complete this task? Some how it feels unsafe since use 2 iterator without any validation.

\$\endgroup\$
5
  • \$\begingroup\$ I'm voting to close this question as off-topic because it could be solved by looking at the public online documentation. \$\endgroup\$ Sep 1, 2017 at 10:29
  • \$\begingroup\$ Is there anyway to complete this task? You mean any better way to complete this task, right? Or isn't this code working yet? \$\endgroup\$
    – t3chb0t
    Sep 1, 2017 at 14:37
  • \$\begingroup\$ @t3chb0t yes. I mistook about the words. Thanks \$\endgroup\$ Sep 2, 2017 at 23:59
  • \$\begingroup\$ @TimothyTruckle It is ok to close, but I would like to know which is the best way to implement that function. In my thought working code and optimized code is different. \$\endgroup\$ Sep 3, 2017 at 0:01
  • \$\begingroup\$ @JuneyoungOh: "but I would like to know which is the best way to implement that function." As the answer of Roland Illig shows there is a better way and you could have found it your self by reading the free online documentation as I mentioned. \$\endgroup\$ Sep 4, 2017 at 6:02

1 Answer 1

5
\$\begingroup\$

There's a simpler way:

Pattern p = Pattern.compile("\\w+@\\{(\\w+)\\}");
Matcher m = p.matcher(targetStr);
while (m.find()) {
    parsed.add(m.group(1));
}

Instead of matching the opening and closing braces separately, the above pattern matches a whole expression of the form aaa@{bbb} at once. And when it finds the expression, it remembers the part inside the braces. This is done via a capturing group (the parentheses).

To learn more about each of the characters in the pattern, read the Javadoc of the Pattern class.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks! I did not know about group methods. \$\endgroup\$ Sep 3, 2017 at 0:02
  • \$\begingroup\$ There's a simpler way: - could you add a short description which way and/or why for poeple - like me ;-) who are unable to find the difference? \$\endgroup\$
    – t3chb0t
    Sep 3, 2017 at 7:30
  • 1
    \$\begingroup\$ @t3chb0t Thanks for the suggestion. It reads much better now. \$\endgroup\$ Sep 3, 2017 at 8:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.