简体   繁体   English

如何将PAN的正则表达式变成渐进式正则表达式

[英]How to make regular expression of PAN to progressive regular expression

I want to make progressive regex of PAN number.我想制作 PAN 号码的渐进式正则表达式。

My PAN number regular expression is "/^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}$/"我的 PAN 号码正则表达式是"/^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}$/"

Format of PAN number -'AAAAADDDDA' A- Alphabets, D- Digits PAN 号码格式 -'AAAAADDDDA' A- 字母,D- 数字

Why I want to make progressive regex?为什么我要制作渐进式正则表达式?

I am using angular directive to set validations on input field.我正在使用角度指令在输入字段上设置验证。 In that I am facing problem to my PAN regex but it works fine for progressive regex.因为我的 PAN 正则表达式面临问题,但它适用于渐进式正则表达式。 Check here my plunker在这里检查我的plunker

Referred question angularjs validate input and prevent change if invalid参考问题angularjs validate input and prevent change if invalid

Can any one please suggest me how to do this?谁能建议我怎么做?

Thank you.谢谢你。

I think this should help you : 我认为这应该对你有所帮助:

regexp = /^([a-zA-Z]([a-zA-Z]([a-zA-Z]([a-zA-Z]([a-zA-Z]([0-9]([0-9]([0-9]([0-9]([a-zA-Z])?)?)?)?)?)?)?)?)?)?$/;

Because you progressively want to test the regex, that's why it test the input letter by letter. 因为你逐渐想要测试正则表达式,这就是为什么它逐字测试输入。 ([a-zA-Z]){5} <- this meant it needs 5 letters (all at same time) because you were testing for every letter input that's why it did not work. ([a-zA-Z]){5} < - 这意味着它需要5个字母(所有同时),因为你正在测试每个字母输入,这就是为什么它不起作用。

For example : 例如 :

AAAAA1111A is correct according to your regex but AAAAA1111 or AAAAA111 or AAAAA11 or AAAAA1 etc are not, that's why your regex did not allow the input! AAAAA1111A根据您的正则表达式是正确的,但AAAAA1111AAAAA111AAAAA11AAAAA1等不是,这就是为什么你的正则表达式不允许输入!

http://plnkr.co/edit/x9x1ZT4InGHHo2f2ZL8f?p=preview http://plnkr.co/edit/x9x1ZT4InGHHo2f2ZL8f?p=preview

for pan validation regex in javascript is 在javascript中用于泛验证正则表达式

let regExpe = /^([AZ]){3}(C|P|H|F|A|T|B|L|J|G){1}([AZ]){1}([0-9]){4}([AZ]){1}?$/; let regExpe = / ^([AZ]){3}(C | P | H | F | A | T | B | L | J | G){1}([AZ]){1}([0-9 ]){4}([AZ]){1} $ /?;

return regExpe.test(pan); return regExpe.test(pan);

Explanation of regular expression 正则表达式的解释

([AZ]){3} :- first three characters can be in between AZ only capital ([AZ]){3}: - 前三个字符可以介于仅AZ资本之间

(C|P|H|F|A|T|B|L|J|G){1} :- fourth character have special meaning (C | P | H | F | A | T | B | L | J | G){1}: - 第四个字符有特殊含义

ie C stands for Corporate, P stands for Personal etc. 即C代表Corporate,P代表Personal等。

([AZ]){1} :- fifth characters can be in between AZ only capital ([AZ]){1}: - 第五个字符可以介于仅AZ资本之间

([0-9]){4} : next four character are numbers so it checks that these four characters should be digits ([0-9]){4}:接下来的四个字符是数字,因此它检查这四个字符应该是数字

([AZ]){1} :- last character also denotes only character in capital ([AZ]){1}: - 最后一个字符也只表示大写字母

after that finally I return the output in true and false by using javascript function 最后我通过使用javascript函数返回true和false的输出

return regExpe.test(pan); return regExpe.test(pan); (it returns true if pan is as per regex else it returns false) (如果pan是regex则返回true,否则返回false)

^([AZ]{3}[P]{1}[AZ]{1}[0-9]{4}[AZ]{1})?$

I can give you this piece of code:我可以给你这段代码:

<input
  data-ng-class="{'invalid-field-box': hasError( 'panCardInput' ) }"
  name="panCardInput"
  class="form-control ng-valid ng-scope ng-touched ng-dirty ng-valid-parse ng-not-empty"
  data-ng-model="registration.panNumber"
  ng-disabled="(alreadyLoggedIn &amp;&amp; ( !profileFromOtherSource || profileConfirmedFlag == 'Y' ) &amp;&amp; ( !profileCreatedByRecruiter || profileConfirmedFlag == 'Y' ) ) ||
RoleQuery.isOfferInitiated() || RoleQuery.isResumeShortlisted()"
  regex="^([A-Z]{3}[P]{1}[A-Z]{1}[0-9]{4}[A-Z]{1})?$"
  regex-msg="PancardCheck.error.regex"
  max-length="10"
  type="text"
  placeholder="Pan Card Number"
  input-validate=""
  style=""
/>

This code is from Tata's website, pretty sure they know what they are doing.此代码来自 Tata 的网站,很确定他们知道自己在做什么。 And they frequently update their validations.他们经常更新他们的验证。

The regex you can use with matches() is formed based on the additional input from the users, and look-behinds check for the preceding 4th character. 可以与matches()一起使用的正则表达式是根据用户的附加输入形成的,并且后台检查前面的第4个字符。 If the 4th letter is P, we check for the first letter in the surname, and if the 4th letter is not P, we check the first letter in the entity name: 如果第4个字母是P,我们检查姓氏中的第一个字母,如果第4个字母不是P,我们检查实体名称中的第一个字母:

String rx = "[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]";

Sample Code 示例代码

String c1 = "S"; // First letter in surname coming from the EditText (with P before)
String c2 = "F"; // First letter in name coming from another EditText (not with P before)
String pan = "AWSPS1234Z"; // true
System.out.println(pan.matches("[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]"));
pan = "AWSCF1234Z"; // true
System.out.println(pan.matches("[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]"));
pan = "AWSCS1234Z"; // false
System.out.println(pan.matches("[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]"));

Click here 点击这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM