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.

t3chb0t
44.6k9 gold badges84 silver badges190 bronze badges
asked Sep 1, 2017 at 9:03
\$\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\$ Commented 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\$ Commented Sep 1, 2017 at 14:37
  • \$\begingroup\$ @t3chb0t yes. I mistook about the words. Thanks \$\endgroup\$ Commented 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\$ Commented 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\$ Commented 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.

answered Sep 1, 2017 at 9:25
\$\endgroup\$
3
  • \$\begingroup\$ Thanks! I did not know about group methods. \$\endgroup\$ Commented 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\$ Commented Sep 3, 2017 at 7:30
  • 1
    \$\begingroup\$ @t3chb0t Thanks for the suggestion. It reads much better now. \$\endgroup\$ Commented Sep 3, 2017 at 8:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.