[英]Regex for string validation
I need a regex for validating the String that comes in pattern like NBTAAA-PREM_001
and NBTAAA_001
我需要一个正则表达式来验证NBTAAA-PREM_001
和NBTAAA_001
等NBTAAA-PREM_001
NBTAAA_001
The NBTAAA
part can be numbers or alphabets or alphanumeric, optionally followed by the fixed string -PREM
(hyphen PREM), then underscore _
, followed by three alphanumeric characters. NBTAAA
部分可以是数字或字母或字母数字,可以选择在其后跟固定字符串-PREM
(连字符PREM),然后在下划线_
后跟三个字母数字字符。
For this "NBTAAA_001", need regex without '-PREM' (hyphen PREM). 对于此“ NBTAAA_001”,需要不带“ -PREM”(连字符PREM)的正则表达式。
I have written the code: 我已经写了代码:
public static void main(String[] args) {
List<String> input = new ArrayList<String>();
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-z0-9]{3})$")) {
System.out.println(" PRIM image value : " + ssn);
}
}
}
else {
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 to resolve this. 请帮助我解决此问题。
^[a-zA-Z0-9]+(?:-PREM)?_[a-zA-Z0-9]{3}$
You can try this.See demo. 您可以尝试一下。请参阅演示。
http://regex101.com/r/iM2wF9/16 http://regex101.com/r/iM2wF9/16
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.