vendredi 27 octobre 2017

Matcher not matching patterns properly

I'm making a program that validates license plates for 31 States, each have different format and go from certain values also they should not include the letters I,O or Q in any of the license plates, here are the formats:

AGUASCALIENTES --from AAA-0001 to AFZ- 9999 

BAJA CALIFORNIA-- from AGA-0001 to CYZ-9999 

BAJA CALIFORNIA SUR-- from  CZA-0001 to DEZ-9999 

CAMPECHE -- from DFA-0001 to DKZ-9999 

CHIAPAS --  from DLA-0001 to DSZ-9999 

CHIHUAHUA -- from   DTA-0001 to ETZ-9999 

COAHUILA -- from EUA-0001 to FPZ-9999 

COLIMA --   from FRA-0001 to FWZ-9999 

DURANGO -- from FXA-0001 to GFZ-9999 

STATE OF MEXICO -- from LGA-0001 to PEZ-9999 

GUANAJUATO -- from  GGA-0001 to GYZ-9999 

GUERRERO -- from GZA-0001 to HFZ-9999 

HIDALGO --  from HGA-0001 to HRZ-9999 

JALISCO -- from HSA-0001 to LFZ-9999 

MICHOACÁN -- from PFA-0001  to  PUZ-9999 

MORELOS --  from PVA-0001   to  RDZ-9999 

NAYARIT -- from REA-0001    to  RJZ-9999 

NUEVO LEÓN -- from  RKA-0001    to  TGZ-999 

OAXACA --   from THA-0001   to  TMZ-9999 

PUEBLA--    from TNA-0001   to  UJZ-9999 

QUERÉTARO-- from    UKA-0001    to  UPZ-9999 

QUINTANA ROO-- from URA-0001    to  UVZ-9999 

SAN LUIS POTOSÍ-- from  UWA-0001    to  VEZ-9999 

SINALOA-- from  VFA-0001    to  VSZ-9999 

SONORA-- from   VTA-0001    to  WKZ-9999 

TABASCO-- from  WLA-0001    to  WWZ-9999 

TAMAULIPAS-- from WXA-0001  to  XSZ-9999 

TLAXCALA-- from XTA-0001    to  XXZ-9999 

VERACRUZ-- from XYA-0001    to  YVZ-9999 

YUCATÁN-- from  YWA-0001    to  ZCZ-9999 

ZACATECAS-- fromZDA-0001    to  ZHZ-9999 

So acording to the format a license plate for Campeche would go like this : DGB-5643

I made these patterns

   Pattern p1 = Pattern.compile("[a][a-z&&[^ioqghjklmnprstuvwxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m1 = p1.matcher(field_val.getText()); // aguascalientes

    Pattern p2 = Pattern.compile("[a|b|c][a-z&&[^ioqhjklmnprstuvwx]][a-z][-]\\d\\d\\d\\d");
    Matcher m2 = p2.matcher(field_val.getText()); // bajac

    Pattern p3 = Pattern.compile("[c|d][a-z&&[^ioqfghjklmnprstuvwxy]][a-z][-]\\d\\d\\d\\d");
    Matcher m3 = p3.matcher(field_val.getText()); // bajasur

    Pattern p4 = Pattern.compile("[d][a-z&&[^ioqabcdelmnprstuvwxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m4 = p4.matcher(field_val.getText()); // campeche

    Pattern p5 = Pattern.compile("[d][a-z&&[^ioqabcdefghjktuvwxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m5 = p5.matcher(field_val.getText()); // chiapas

    Pattern p6 = Pattern.compile("[d|e][a-z&&[^ioqabcdefghjklmnprsuvwxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m6 = p6.matcher(field_val.getText()); // chihuahua

    Pattern p7 = Pattern.compile("[e|f][a-z&&[^ioqabcdefghjklmnvwxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m7 = p7.matcher(field_val.getText()); // coahuila

    Pattern p8 = Pattern.compile("[f][a-z&&[^ioqabcdefghjklmnpxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m8 = p8.matcher(field_val.getText()); // colima

    Pattern p9 = Pattern.compile("[f|g][a-z&&[^ioqghjklmnprstuvw]][a-z][-]\\d\\d\\d\\d");
    Matcher m9 = p9.matcher(field_val.getText()); // durango

    Pattern p10 = Pattern.compile("[l|m|n|p][a-z&&[^ioqf]][a-z][-]\\d\\d\\d\\d");
    Matcher m10 = p10.matcher(field_val.getText()); // edomex

    Pattern p11 = Pattern.compile("[g][a-z&&[^ioqabcdefz]][a-z][-]\\d\\d\\d\\d");
    Matcher m11 = p11.matcher(field_val.getText()); // guanajuato

    Pattern p12 = Pattern.compile("[g|h][a-z&&[^ioqghjklmnprstuvwxy]][a-z][-]\\d\\d\\d\\d");
    Matcher m12 = p12.matcher(field_val.getText()); // guerrero

    Pattern p13 = Pattern.compile("[h][a-z&&[^ioqabcdefstuvwxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m13 = p13.matcher(field_val.getText()); // hidalgo

    Pattern p14 = Pattern.compile("[h|j|k|l][a-z&&[^ioqabcdetuvwxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m14 = p14.matcher(field_val.getText()); // jalisco

    Pattern p15 = Pattern.compile("[p][a-z&&[^ioqabcdevwxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m15 = p15.matcher(field_val.getText()); // michoacan

    Pattern p16 = Pattern.compile("[p|r][a-z&&[^ioqefghjklmnprstu]][a-z][-]\\d\\d\\d\\d");
    Matcher m16 = p16.matcher(field_val.getText()); // morelos

    Pattern p17 = Pattern.compile("[r][a-z&&[^ioqabcdklmnprstuvwxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m17 = p17.matcher(field_val.getText()); // nayarit

    Pattern p18 = Pattern.compile("[r|s|t][a-z&&[^ioqhj]][a-z][-]\\d\\d\\d\\d");
    Matcher m18 = p18.matcher(field_val.getText()); // nuevoleon

    Pattern p19 = Pattern.compile("[t][a-z&&[^ioqabcdefgnprstuvwxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m19 = p19.matcher(field_val.getText()); // oaxaca

    Pattern p20 = Pattern.compile("[t|u][a-z&&[^ioqklm]][a-z][-]\\d\\d\\d\\d");
    Matcher m20 = p20.matcher(field_val.getText()); // puebla

    Pattern p21 = Pattern.compile("[u][a-z&&[^ioqabcdefghjrstuvwxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m21 = p21.matcher(field_val.getText()); // queretaro

    Pattern p22 = Pattern.compile("[u][a-z&&[^ioqabcdefghjklmnpwxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m22 = p22.matcher(field_val.getText()); // quintanaroo

    Pattern p23 = Pattern.compile("[u|v][a-z&&[^ioqfghjklmnprstuv]][a-z][-]\\d\\d\\d\\d");
    Matcher m23 = p23.matcher(field_val.getText()); // sanluispotosi

    Pattern p24 = Pattern.compile("[v][a-z&&[^ioqabcdetuvwxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m24 = p24.matcher(field_val.getText()); // sinaloa

    Pattern p25 = Pattern.compile("[v|w][a-z&&[^ioqlmnprs]][a-z][-]\\d\\d\\d\\d");
    Matcher m25 = p25.matcher(field_val.getText()); // sonora

    Pattern p26 = Pattern.compile("[w][a-z&&[^ioqabcdefghjkxyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m26 = p26.matcher(field_val.getText()); // tabasco

    Pattern p27 = Pattern.compile("[w|x][a-z&&[^ioqtuvw]][a-z][-]\\d\\d\\d\\d");
    Matcher m27 = p27.matcher(field_val.getText()); // tamaulipas

    Pattern p28 = Pattern.compile("[x][a-z&&[^ioqabcdefghjklmnprsyz]][a-z][-]\\d\\d\\d\\d");
    Matcher m28 = p28.matcher(field_val.getText()); // tlaxcala

    Pattern p29 = Pattern.compile("[x][a-z&&[^ioqwx]][a-z][-]\\d\\d\\d\\d");
    Matcher m29 = p29.matcher(field_val.getText()); // veracruz

    Pattern p30 = Pattern.compile("[y|z][a-z&&[^ioqdefghjklmnprstuv]][a-z][-]\\d\\d\\d\\d");
    Matcher m30 = p30.matcher(field_val.getText()); // yucatan

    Pattern p31 = Pattern.compile("[z][a-z&&[^ioqabcjklmnprstuvwxy]][a-z][-]\\d\\d\\d\\d");
    Matcher m31 = p31.matcher(field_val.getText()); // zacatecas

I'm sending the result to a textfield called res

here are the matchers

 if (m1.find()) {

        res.setText(" Your State is  Aguascalientes");
    } else if (m2.find()) {

        res.setText(" Your State is  Baja");
    } else if (m3.find()) {

        res.setText(" Your State is  Baja Sur");
    } else if (m4.find()) {

        res.setText(" Your State is  Campeche");
    } else if (m5.find()) {

        res.setText(" Your State is  Chiapas");
    } else if (m6.find()) {

        res.setText(" Your State is  Chihuahua");
    } else if (m7.find()) {

        res.setText(" Your State is  Coahuila");
    } else if (m8.find()) {

        res.setText(" Your State is  Colima");
    } else if (m9.find()) {

        res.setText(" Your State is  Durango");
    } else if (m10.find()) {

        res.setText(" Your State is  Edomex");
    } else if (m11.find()) {

        res.setText(" Your State is  Guanajuato");
    } else if (m12.find()) {

        res.setText(" Your State is  Guerrero");
    } else if (m13.find()) {

        res.setText(" Your State is  Hidalgo");
    } else if (m14.find()) {

        res.setText(" Your State is  Jalisco");
    } else if (m15.find()) {

        res.setText(" Your State is  Michoacan");
    } else if (m16.find()) {

        res.setText(" Your State is  Morelos");
    } else if (m17.find()) {

        res.setText(" Your State is  Nayarit");
    } else if (m18.find()) {

        res.setText(" Your State is  Nuevo Leon");
    } else if (m19.find()) {

        res.setText(" Your State is  Oaxaca");
    } else if (m20.find()) {

        res.setText(" Your State is  Puebla");
    } else if (m21.find()) {

        res.setText(" Your State is  Queretaro");
    } else if (m22.find()) {

        res.setText(" Your State is  Quintana Roo");
    } else if (m23.find()) {

        res.setText(" Your State is  San Luis Potosi");
    } else if (m24.find()) {

        res.setText(" Your State is  Sinaloa");
    } else if (m25.find()) {

        res.setText(" Your State is  Sonora");
    } else if (m26.find()) {

        res.setText(" Your State is  Tabasco");
    } else if (m27.find()) {

        res.setText(" Your State is  Tamaulipas");
    } else if (m28.find()) {

        res.setText(" Your State is  Tlaxcala");
    } else if (m29.find()) {

        res.setText(" Your State is  Veracruz");
    } else if (m30.find()) {

        res.setText(" Your State is  Yucatan");
    } else if (m31.find()) {

        res.setText(" Your State is  Zacatecas");
    }

In cases where the first Letter Repeats in two or three states i don't get the correct match for the state i want

For example i input ABH-6262 and i get Your State is Baja California as a result and i'm expecting to get Aguascalientes

Aucun commentaire:

Enregistrer un commentaire