-1

I want a pattern like this: GJ-16-RS-1234 and I have applied following patterns but they are not working.

My regex patterns are:

String str_tempPattern = "(^[A-Z]{2})\\-([0-9]{2})\\-([A-Z]{1,2})\\-([0-9]{1,4}$)";

String str_tempPattern = "(^[A-Z]{2})-([0-9]{1,2})-([A-Z]{1,2})-([0-9]{1,4})$";

String str_tempPattern = "^[A-Z]{2}\\-[0-9]{1,2}\\-[A-Z]{1,2}\\-[0-9]{1,4}$";

And I am using text watcher to check for any change in the aftertextchange()

Pattern p = Pattern.compile(str_tempPattern, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    Matcher m = p.matcher(s);

    if (m.find()){

    }
Jojodmo
  • 23,357
  • 13
  • 65
  • 107
Ravi Yadav
  • 2,296
  • 3
  • 25
  • 32

1 Answers1

8

Just set the condition using matches method.

if (string.matches("[A-Z]{2}\\-[0-9]{1,2}\\-[A-Z]{1,2}\\-[0-9]{1,4}")) 
{
        // Yes it matches
}
else
{
       // No it won't
}
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274