nop :
I want to make a pattern matching for the following:
FirstName LastName (ID: 1)
Code:
Pattern pattern = Pattern.compile("^([A-Za-z0-9_ ].*)(ID: (?<customerId>[0-9].*))$");
Matcher matcher = pattern.matcher(customer.getSelectedItem().toString());
if (matcher.matches()) {
String customerId = matcher.group("customerId"); // returns 1) instead of just 1
...
The problem is that my regex expression returns 1)
instead of just 1
or any number.
Toto :
Remove the .*
tahtt means 0 or more any character (including the closing parenthesis):
Pattern.compile("^([A-Za-z0-9_( ]*)(ID: (?<customerId>[0-9]*))\\)$");
You can also remove the groups if you don't want to capture:
Pattern.compile("^[\\w ]*\\(ID: (?<customerId>[0-9]*)\\)$");