Answer the question
In order to leave comments, you need to log in
How to properly format a regular expression in Java?
There is a line like "ping pong 123".
There is a regular expression:
Pattern p = Pattern.compile("ping\s*(pong\s*)?(?<SimpleText>.+)?");
Matcher m = p.matcher("ping pong 123");
System.out.println(m.group("SimpleText")); // ожидается 123
Matcher m = p.matcher("ping 123");
System.out.println(m.group("SimpleText")); // ожидается 123
Matcher m = p.matcher("ping pong");
System.out.println(m.group("SimpleText")); // ожидается null; по факту: "pong"
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question