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.
-
\$\begingroup\$ I'm voting to close this question as off-topic because it could be solved by looking at the public online documentation. \$\endgroup\$Timothy Truckle– Timothy Truckle2017年09月01日 10:29:08 +00:00Commented 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– t3chb0t2017年09月01日 14:37:44 +00:00Commented Sep 1, 2017 at 14:37
-
\$\begingroup\$ @t3chb0t yes. I mistook about the words. Thanks \$\endgroup\$Juneyoung Oh– Juneyoung Oh2017年09月02日 23:59:37 +00:00Commented 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\$Juneyoung Oh– Juneyoung Oh2017年09月03日 00:01:46 +00:00Commented 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\$Timothy Truckle– Timothy Truckle2017年09月04日 06:02:32 +00:00Commented Sep 4, 2017 at 6:02
1 Answer 1
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.
-
\$\begingroup\$ Thanks! I did not know about
group
methods. \$\endgroup\$Juneyoung Oh– Juneyoung Oh2017年09月03日 00:02:27 +00:00Commented 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– t3chb0t2017年09月03日 07:30:10 +00:00Commented Sep 3, 2017 at 7:30
-
1\$\begingroup\$ @t3chb0t Thanks for the suggestion. It reads much better now. \$\endgroup\$Roland Illig– Roland Illig2017年09月03日 08:56:30 +00:00Commented Sep 3, 2017 at 8:56