Regex for string validation -
I need a regex to confirm the string, which is
NBTAAA-PREM_001 and
NBTAAA_001 NBTAAA can be part number or alpha or alphanumeric, optionally defined string -PREM (hyphen PREM) , Then underscore the _ after three alphanumeric characters. For this "NBTAAA_001", regex is required without 'PREM' (hyphen PREM).
I have written the code:
public static zero main (string [] args) {list & lt; String & gt; Input = new arrelist & lt; String & gt; (); Input.add ("NBTAAA-PRIM_001"); input.add ("NBTAAA_001"); For (string SSN: input) {// string SSN = "NBTAAA-PRIM_001"; If (ssn.indexOf ("-")! = -1) {int PRIMbeginIndex = ssn.indexOf ("-") + 1; Int PRIMEndIndex = PRIMbeginIndex + 4; String D = ssn.substring (PRIMbeginIndex, PRIMEndIndex); If (d.equalsIgnoreCase ("PRIM") {if (ssn.matches ("^ ([A-Za-z0- 9] + (? =. *? \ BPRIM \ b) + _ [A-za-z 0 -9] {3}) $ ")) {System.out.println (" PRIM Image Value: "+ SSN); }}} And {if (ssn.matches ("^ ([A-Za-z0-9] + _ [A-Za-z0- 9] {3}) $")) {System.out.println (" Normal image value: "+ SSN); }}}} But it is not working properly. Please help me solve it.
^ [a-zA-Z0- 9] + (?: - PREM)? _ [A-zA-Z0- 9] {3} $ You can try it. See the demo.
Comments
Post a Comment