user964283
user964283

Reputation: 863

Unicode regular expression in Java

I need to write a regular expression in Java which covers the following scenarios

Between 6-10 characters

Must be at least one numeral

I have been told that I should use Unicode convention in the regular expression as the input may be in many languages

Can anyone help?

Upvotes: 0

Views: 160

Answers (1)

fge
fge

Reputation: 121790

For string length, just use the .length() method. For matching, use \p{Digit}, this should do what you want and covers more than \d (although I fail to see what).

So:

final Pattern p = Pattern.compile("\\p{Digit}");

//...

if (input.length() < 6 || input.length() > 10)
    //fail
if (!p.matcher(input).find())
    //fail

// Success!

Upvotes: 1

Related Questions