這是代碼:
String newtask = "newTask[\\{] taskId\"(?<id>\\d{1,4})\", username\"\\w([_\\w\\d&&^(bahmanBAHMAN)]){6,15}\\d\", isValid\"(?<isitthough>(no|yes))\", name\"[\\w\\s]*\", address\"(?<address>[\\w\\s] [-]\\d{8,10})\", date\"(?<date>\\d{8})\", info\"(?<info>[^\"]*)\", price\"(?<price>\\d \\.{0,1}\\d*)\" "; String changeaddress = "changePlace[\\{] taskId\"(?<idtobechanged>\\d{1,4})\", newAddress\"(?<theaddress>[\\w\\s] [-]\\d{8,10})\" ";
Pattern addingpat = Pattern.compile(newtask);
String command=scanner.nextLine();
Matcher addingmat = addingpat.matcher(command);
if (addingmat.find())
System.out.println("it has matched");
以及用戶將在終端中輸入的內容:
newTask{ taskId"1", username"AyesItsGoood4", isValid"yes", name"reza", address"LeninGrad-12345678", date"19410908", info"do the task", price"1.57"
它沒有列印任何東西。如果有人指出這段代碼的問題,我將非常感激。謝謝閱讀。
uj5u.com熱心網友回復:
在您嘗試使用字符類減法的地方需要修復正則運算式:
newTask[\{] taskId\"(?<id>\d{1,4})\", username\"[a-zA-Z][a-zA-Z&&[^bahmnBAHMN]]{6,15}\d\", isValid\"(?<isitthough>no|yes)\", name\"[\w\s]*\", address\"(?<address>[\w\s] -\d{8,10})\", date\"(?<date>\d{8})\", info\"(?<info>[^\"]*)\", price\"(?<price>\d \.?\d*)\"
請參閱正則運算式演示。
\w([_\w\d&&^(bahmanBAHMAN)]){6,15}\d\"替換為(?!(?i:bahman\"))\w{7,16}\d\"。原因如下:
\w([_\w\d&&^(bahmanBAHMAN)]){6,15}\d- 匹配一個單詞 char(帶\w),然后出現 6-15 個b,a,h,m,n,B,A,H,M,Nchar 和一個數字。發生這種情況是因為您使用的是字符類交集,并且該[_\w\d&&^(bahmanBAHMAN)]模式僅匹配同時存在于[_\w\d]和字符集中的那些[^(bahmanBAHMAN)]字符。- 該
[a-zA-Z][a-zA-Z&&[^bahmnBAHMN]]{6,15}\d模式匹配單個 ASCII 字母,然后是 6 到 15 個 ASCII 字母b(不區分大小寫)和一個數字a。hmn
在 Java 中:
String newtask = "newTask\\{ taskId\"(?<id>\\d{1,4})\", username\"[a-zA-Z][a-zA-Z&&[^bahmnBAHMN]]{6,15}\\d\", isValid\"(?<isitthough>no|yes)\", name\"[\\w\\s]*\", address\"(?<address>[\\w\\s] -\\d{8,10})\", date\"(?<date>\\d{8})\", info\"(?<info>[^\"]*)\", price\"(?<price>\\d \\.?\\d*)\"";
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/444344.html
