Java 想要一个用于验证印度车辆编号格式的正则表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6386300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:30:28  来源:igfitidea点击:

Want a regex for validating Indian Vehicle Number Format?

javaregexvalidation

提问by Asif

Hello everyone here...

大家好,这里...

I need to build up a swing application related to vehicle registration and in which i want to do input the vehicle number of indian standard, like:

我需要建立一个与车辆登记相关的摇摆应用程序,我想在其中输入印度标准的车辆编号,例如:

MP 09 AB 1234

AH 17 FT 2387

UT 32 DR 6423

DL 01 C AA 1111

MP 09 AB 1234

AH 17 英尺 2387

UT 32 DR 6423

DL 01 C AA 1111

More Specifically,

进一步来说,

Please if any one can help me? DocumentFiltertype class can also help me.........

请如果有人可以帮助我吗? DocumentFiltertype 类也可以帮我.........

采纳答案by Alix Axel

Based on the Wikipedia spec:

基于维基百科规范

^[A-Z]{2}[ -][0-9]{1,2}(?: [A-Z])?(?: [A-Z]*)? [0-9]{4}$
  • The first two letters of the registration plate represent the State in which the vehicle is Registered.
  • The next two digit numbers are the sequential number of a district. Due to heavy volume of vehicle registration, the numbers were given to the RTO offices of registration as well.
  • The third part is a 4 digit number unique to each plate. A letter(s) is prefixed when the 4 digit number runs out and then two letters and so on.
  • In some states (such as the union territory of Delhi, and the state of Gujarat) the initial 0 of the district code is omitted; thus Delhi district 2 numbers appear as DL 2 not DL 02.
  • The National Capital Territory of Delhi has an additional code in the registration code: DL 01 C AA 1111
  • 车牌的前两个字母代表车辆注册所在的州。
  • 接下来的两位数字是一个区的序号。由于车辆登记量很大,这些号码也提供给 RTO 登记处。
  • 第三部分是每个板唯一的 4 位数字。一个或多个字母在 4 位数字用完时加上前缀,然后是两个字母,依此类推。
  • 在某些州(例如德里的联邦领土和古吉拉特邦),地区代码的首字母 0 被省略;因此,德里 2 区号码显示为 DL 2 而不是 DL 02。
  • 德里国家首都辖区在注册码中多了一个代码:DL 01 C AA 1111

回答by MoarCodePlz

Try this

尝试这个

^[A-Z]{2}\s[0-9]{2}\s[A-Z]{2}\s[0-9]{4}$

^means start of string
[A-Z]{2}means 2 characters in the range of A through Z
\smeans white space
[0-9]{2}means 2 characters in the range of 0 through 9
\smeans white space
[A-Z]{2}means 2 characters in the range of A through Z
\smeans white space
[0-9]{4}means 4 characters in the range of 0 through 9
$means end of string

^表示字符串的开头
[A-Z]{2}表示 A 到 Z 范围内的 2 个字符
\s表示空白
[0-9]{2}表示 0 到 9 范围内的2 个字符
\s表示空白
[A-Z]{2}表示 A 到 Z 范围内的 2 个字符
\s表示空白
[0-9]{4}表示范围内的 4 个字符0 到 9
$表示字符串结束

Based on what you said in your question this should be a very broad check against proper format, but I have a feeling that there are more specific regulations on how the license plates are numbered. Let me know if there are additional constraints for the regex.

根据您在问题中所说的内容,这应该是对正确格式的非常广泛的检查,但我觉得对于车牌的编号方式有更具体的规定。让我知道正则表达式是否有其他限制。

回答by K6t

just check it out for whitespace only...

只需检查一下是否有空格...

  function isValidNumber($Number){
         $pattern = "^[a-zA-z]{2}\s[0-9]{2}\s[0-9]{4}$";
         if (eregi($pattern, $Number)){
            return true;
         }
         else {
            return false;
        }
    }

回答by Fredrik Pihl

^[A-Z]{2} [0-9]{2} [A-Z]{2} [0-9]{4}$

See http://en.wikipedia.org/wiki/Vehicle_registration_plates_of_India

http://en.wikipedia.org/wiki/Vehicle_registration_plates_of_India

Looks like the format is a bit more complex than stated in your question...

看起来格式比您的问题中陈述的要复杂一些......

回答by Asif

Say for example if the user already entered a value like:

例如,如果用户已经输入了一个值,例如:

DL 

then the textbox did not take the next input any other than a integer or one more char as.....

然后文本框没有将下一个输入作为整数或多一个字符作为......

DL C 

from here user can only enter a integer value............

从这里用户只能输入一个整数值........

So, in short what i want to do is to take the input only like that regex given by Alix

所以,简而言之,我想要做的就是只像 Alix 给出的正则表达式一样接受输入

Based on the Wikipedia Specs

基于维基百科规范

^[A-Z]{2}[ -][0-9]{1,2}(?: [A-Z])?(?: [A-Z]*)? [0-9]{4}$

回答by Sachin Daingade

-(BOOL) validateVehicleRegNumber:(NSString *)strNumber { //Example MH14DA8904

-(BOOL) validateVehicleRegNumber:(NSString *)strNumber { //示例 MH14DA8904

    NSString *validReg = @"[A-Z]{2}[0-9]{2}[A-Z]{2}[0-9]{4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", validReg];
    return [emailTest evaluateWithObject:strNumber];

}

}

回答by Ketan sangle

^[A-Z]{2}\s[0-9]{1,2}\s[A-Z]{1,2}\s[0-9]{1,4}$ 

The above regex will work for the following types

上述正则表达式适用于以下类型

DL 01 AA 1111

DL 01 AA 1111

DL 0 AA 1111

DL 0 AA 1111

DL 1 A 1111

DL 1 A 1111

DL 0 A 11

DL 0 A 11

DL 01 AA 111

DL 01 AA 111

回答by Manoj Chavanke

//DL 01 C AA 1234
^[A-Z]{2}[ -]{0,1}[0-9]{2}[ -]{0,1}(?:[A-Z])[ -]{0,1}[A-Z]{1,2}[ -]{0,1}[0-9]{1,4}$

//MH 15 AA 1234
^[A-Z]{2}[ -]{0,1}[0-9]{2}[ -]{0,1}[A-Z]{1,2}[ -]{0,1}[0-9]{1,4}$

MVA 1234
^[A-Z]{3}[ -]{0,1}[0-9]{1,4}$

//MH 15 8008
^[A-Z]{2}[ -]{0,1}[0-9]{2}[ -]{0,1}[0-9]{1,4}$

with or without space :)

有或没有空间:)

回答by ked

If you are looking for number plate in substring then use the following regex

如果您要在子字符串中查找车牌号,请使用以下正则表达式

.*?([A-Za-z]{2})(\d{2})([A-Za-z]{2})(\d{4}).*?