2

I have the following code...

private static final String REGEX_TEAMSPLIT =
 "([0-9]*)\\s-\\s([A-Z,a-z][a-z,\\s,A-Z]*)";
...
String reg = teamNames.get(0).html();
//reg == '62401 - Breakers'
teamNumber = reg.replace(REGEX_TEAMSPLIT, "$1");
//teamNumber == '62401 - Breakers'

Now I was assuming this would leave me with only the first group per what I read online. But as you can see this is not the case. Can someone tell what I am missing?

1 Answer 1

3

You should use replaceAll instead of replace. (replace method does not interpret the first parameter as regular expression, but matches literally)

String REGEX_TEAMSPLIT = "([0-9]*)\\s-\\s([A-Z,a-z][a-z,\\s,A-Z]*)";
String reg = "62401 - Breakers";
String teamNumber = reg.replaceAll(REGEX_TEAMSPLIT, "$1");
System.out.println(teamNumber); // => 62401
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.