Alex Rivera | Logout

Regex match digits, comma and semicolon?

Asked 2011-03-16T15:03:45.543
43

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.

Edit
Report

1 Answer

6

You were almost there, you may use this regex solution:

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

RegEx Details:

  • ^: Start
  • [0-9,;]+: Match 1+ of a digit or , or ; characters
  • $: End

Note that when we use .matches() start and end anchors are implied since it only matches complete input so you may omit anchors in your regex to make it:

word.matches("[0-9,;]+");
answered 2011-03-16T15:06:19.673

Your Answer