Reputation: 1
I'm fairly new to Regex, but not new to Java as a coding language. I'm currently trying to create a Regex expression that will format a user's input to two separate values, but I'm a little curious as to how to approach it.
For example, suppose a user were guessing the resulting score of a basketball game, there's a handful of formats they could use:
57-89
57:89
57/89
etc.
I guess my question is first, how would I go about having my Regex expression handle multiple digits? That is, recognizing a valid guess regardless of how many digits they were to put in for each value. Second of all, how would I go about creating a Regex expression that would handle multiple formats, such as the ones listed above?
Thanks ahead of time.
Upvotes: 0
Views: 64
Reputation: 2583
If the input is in the following format:
<integer><non-integer delimiter><integer>
then this split
method will parse it into a String[] with each integer as a separate element:
inputString.split("[^0-9]+");
[^0-9]+
is the regex for the delimiter:
[]
character class;^
exclude the following characters;0-9
character range 0, 1, ..., 9;+
one or more occurrences (that means it will work for multicharacter delimiter, e.g. 59 - 87
).More information on Java regexes is here.
Upvotes: 1