javaregex

Regex match digits, comma and semicolon?


What's a regular expression that will match a string only containing digits 0 through 9, a comma, and a semi-colon? I'm looking to use it in Java like so:

word.matches("^[1-9,;]$") //Or something like that...

I'm new to regular expressions.


Solution

  • You almost have it, you just left out 0 and forgot the quantifier.

    word.matches("^[0-9,;]+$")