[英]Regex for string validation
我需要一個正則表達式來驗證NBTAAA-PREM_001
和NBTAAA_001
等NBTAAA-PREM_001
NBTAAA_001
NBTAAA
部分可以是數字或字母或字母數字,可以選擇在其后跟固定字符串-PREM
(連字符PREM),然后在下划線_
后跟三個字母數字字符。
對於此“ NBTAAA_001”,需要不帶“ -PREM”(連字符PREM)的正則表達式。
我已經寫了代碼:
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);
}
}
}
}
但是它不能正常工作。 請幫助我解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.