我正在努力使用正則運算式。我這里有一個例子!
RegExp timeExp = RegExp(r"^[0-9]{1,2}?:[0-9]{0,2}");
String time1 = "12"; // should be valid
String time2 = "12:30"; // should be valid
String time3 = "123:20:"; // should be valid
String time4 = "123::20:"; // should NOT be valid
String time5 = "12::20:"; // should NOT be valid
String time5 = "1:20:"; // should be valid
if(timeExp.hasMatch(time1)){
print('time1 is valid');
}
if(timeExp.hasMatch(time2)){
print('time2 is valid');
}
if(timeExp.hasMatch(time3)){
print('time3 is valid');
}
if(timeExp.hasMatch(time4)){
print('time4 is valid');
}
if(timeExp.hasMatch(time5)){
print('time5 is valid');
}
正則運算式作業不正常。有人可以幫忙嗎?
提前致謝。
uj5u.com熱心網友回復:
您可以使用
^[0-9]{1,3}(?::[0-9]{1,3})*:?$
請參閱正則運算式演示。詳情:
^- 字串的開始[0-9]{1,3}- 一位、兩位或三位數字(?::[0-9]{1,3})*- 零次或多次出現 a:和一位、兩位或三位數字:?- 一個可選的:$- 字串的結尾。
另一種可能的解決方案是
^(?:[0-9]{1,3}(?::|$)) $
請參閱此正則運算式演示,其中(?:[0-9]{1,3}(?::|$)) 匹配一個或多個出現的一到三個數字,后跟:或$。
如果您需要匹配數字部分中的任意數量的數字,請替換{1,3}為 。
uj5u.com熱心網友回復:
使用您顯示的示例,請嘗試以下正則運算式。
^(?:[0-9]{1,3})(?::[0-9]{1,3}[:]?)?$
上述正則運算式的在線演示
說明:為上述正則運算式添加詳細說明。
^(?:[0-9]{1,3}) ##Matching from starting of value in a non-capturing group 1 to 3 digits here.
(?: ##Starting another non-capturing group from here.
:[0-9]{1,3} ##Matching colon followed by 1 to 3 occurrences of digits here.
[:]? ##Matching 0 or 1 colon occurrences here.
)?$ ##Keeping this non-capturing group as optional at end of value.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/393783.html
