-3

I'm trying to verify that a user inputs coordinates in the format: x,y so I'm using regular expressions. x and y can be between 1 and 15, so I used

[1-15]\\d{1,2},[1-15]\\d{1,2}

but that didn't work because 15 isn't a digit obviously. I changed it to

\\d{1,2},\\d{1,2}

so at least I can confirm that its two one or two digit numbers, but it could be up to 99 on either side; not good. I've tried a few other ways like

\\d{1}|[1]\\d[0-5]\\d...

but nothing works and honestly I've been looking at all this so long it doesn't make sense anymore.

More importantly, is it even good practice to use java's regular expression feature? I could think of other ways to do this, but this is just for a personal project I'm working on and trying out different approaches to things I usually do messily.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
North
  • 109
  • 2
  • 12

1 Answers1

1

I think you understand the [...] the wrong way: it means you specify a range of characters. So [1-15] means: 1to 1and 5. It is thus equivalent to 1|5.

You can however specify the digits 1 to 15 with [1-9]|1[0-5]. Plugging this into your regex results in:

([1-9]|1[0-5])\\d{1,2},([1-9]|1[0-5])\\d{1,2}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555