Hello. Began to learn Java recently, for this I can not cope. Help make a regular expression, which will need to be compared with a string, and check if there is a match with (Number1 + Number2). If there is - pull out these numbers. Thank you in advance!
|
1 answer
You can try this:
public static void checkWithRegExp(String str){ Pattern p = Pattern.compile("^(\\d+)\\+(\\d+)$"); Matcher m = p.matcher(str); if (m.find()) { System.out.println("Совпадение обнаружено!"); for(int i = 1; i <= m.groupCount(); i++) { System.out.println("Число: " + m.group(i)); } } else { System.out.println("Совпадений не обнаружено!"); } } public static void main(String[] args) { checkWithRegExp("3+2342"); } In the case of the parameter "3+2342" output will be
A match is found!
Number: 3
Number: 2342
in the case of "31230+" output will be
No matches found!
- @SamyRed, if the answer helped you, do not forget to accept it (green check mark under the voting buttons) - Nofate ♦
|