匹配器未正确匹配模式

时间:2017-10-28 04:02:18

标签: java regex matcher

我正在制作一个程序来验证31个国家的牌照,每个国家都有不同的格式,并且从某些值开始,它们也不应该包含任何牌照中的字母I,O或Q,以下是格式:

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 

根据格式,坎佩切的车牌会是这样的: DGB-5643

我制作了这些模式

   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

我将结果发送到名为textfield

res

这里是匹配者

 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");
    }

如果第一个字母重复处于两个或三个状态,我没有得到我想要的状态的正确匹配

例如我输入ABH-6262,结果得到Your State is Baja California,我期待得到Aguascalientes

3 个答案:

答案 0 :(得分:1)

我假设您在使用这些模式匹配器之前将所有输入字符串转换为小写字母。 (令人困惑的是,你已经忘记了这一点,因为你在谈论大写的车牌号码,但只匹配小写字母。)

我不明白为什么ABH-6262会给你错误的结果。但是,肯定会产生错误结果的组合。例如,您说Edomex应该是从LGA-0001到PEZ-9999。但是,你的模式

Pattern p10 = Pattern.compile("[l|m|n|p][a-z&&[^ioqf]][a-z][-]\\d\\d\\d\\d");

将接受任何首字母为L,M,N或P的组合,这是好的,但也会接受第二个字母不是F(或I,O,Q)的任何组合。这意味着它将接受属于哈利斯科州的LAA;并且它不接受MFA,它应该是Edomex的一部分。

问题是你要比较三个字母和#34;单词"按字母顺序排列,正则表达式是该工作的错误工具。您可以使用类似

的模式执行此操作
(l[g-z&&[^ioq]]|[mn][a-z&&[^ioq]]|p[a-e])[a-z&&[^ioq]]-\d\d\d\d

括号中的部分使用|个字符,表示它可以匹配其中一个备选项。在这种情况下,如果下列之一为真(如果没有字母是I,O或Q),它会匹配车牌的前两个字母:

  • 第一个字母是L,第二个字母是G到Z;
  • 第一个字母是M或N,第二个字母是任何东西;
  • 第一个字母是P,第二个字母是A到E.

您必须重写许多匹配器才能执行此类操作。但这会使事情变得复杂得多。事实是,你应该使用字符串比较而不是模式匹配器来完成你想要做的事情。

您只需一种模式即可完成此操作:

Pattern licensePlatePattern = Pattern.compile("([a-z&&[^ioq]]{3})-\\d{4}");

注意事项:第一部分匹配任何不是I,O或Q的三个字母;但是这个模式的一部分在括号中,它创建了一个,以便我们可以稍后查看这三个字母。请注意,这也使用{3}来表示其前面的三个内容(类似{4})。另请注意,我自己使用-来匹配短划线,而不是[-];在匹配一个特定字符时,您不需要使用[]

现在:

Matcher matcher = licensePlatePattern.matcher(field_val.getText());
if (matcher.find()) {
    String letters = matcher.group(1);
        // This gets the part of the input that matches the first parenthesized
        // part of the pattern (in this case, the only parenthesized part)
    if (letters.compareTo("aaa") >= 0 && letters.compareTo("afz") <= 0) then {
        res.setText(" Your State is  Aguascalientes");
    } else if (letters.compareTo("aga") >= 0 && letters.compareTo("cyz") <= 0) then {
        res.setText(" Your State is  Baja");
    } else if (letters.compareTo("cza") >= 0 && letters.compareTo("dez") <= 0) then {
        res.setText(" Your State is  Baja Sur");

等等。这比拥有31种复杂模式更容易理解。更好的方法是将状态信息放在一个数组中,比如

public class StateInfo {
    private String name;
    private String lowestThreeLetters;
    private String highestThreeLetters;
    // constructor, getters -- I'll let you figure out how to write these
}

// in some other class:
public static final StateInfo[] stateInfo = new StateInfo[] {
    new StateInfo("Aguascalientes", "aaa", "afz"),
    new StateInfo("Baja", "aga", "cyz"),
    new StateInfo("Baja Sur", "cza", "dez"),
    ... };

而不是31个if语句,您可以遍历数组:

    String letters = matcher.group(1);
    for (StateInfo s : stateInfo) {
        if (letters.compareTo(s.getLowestThreeLetters()) >= 0 &&
            letters.compareTo(s.getHighestThreeLetters()) <= 0) { 
            res.setText(" Your State is  " + s.getName());
            break;
        }
    }

答案 1 :(得分:0)

如果您想同时检查大写和小写,可以使用它

Pattern p1 = Pattern.compile("[a|A][a-f[A-F]][a-z[A-Z]&&[^ioqIOQ]][-][0-9][0-9][0-9][1-9]");
Matcher m1 = p1.matcher(field_val); // aguascalientes

Pattern p2 = Pattern.compile("[a-c[A-C]][g-y[G-Y]&&[^ioqIOQ]][a-z[A-Z]&&[^ioqIOQ]][-][0-9][0-9][0-9][1-9]");
Matcher m2 = p2.matcher(field_val); // bajac

答案 2 :(得分:-1)

这对我有用。但是,没有检查细节。但是你的模式可以稍微提高一点。试试这个模式:

Pattern p1 = Pattern.compile("[a|A][a-f][a-z&&[^ioq]][-][0-9][0-9][0-9][1-9]");
Matcher m1 = p1.matcher(field_val); // aguascalientes

Pattern p2 = Pattern.compile("[a-c][g-y&&^[ioq]][a-z&&[^ioq]][-][0-9][0-9][0-9][1-9]");
Matcher m2 = p2.matcher(field_val);

-jahanvi.bansal@gmail.com