我有一個具有以下格式的字符串:111111#1數字字符的數量是 5 或 6,之后我設置了一個 '#'并設置一個數字字符。我像這樣使用 Regex.IsMatch :if (Regex.IsMatch(string, @"^d{6}#\d{1}"))
{...}但它無法處理我的字符串 我的錯誤是什么?
2 回答

萬千封印
TA貢獻1891條經驗 獲得超3個贊
此單行正則表達式將捕獲兩組:前五到六位數字和“#”。后跟一個數字:
(\d{5,6})(#\d{1})
例子:
string pattern = @"(\d{5,6})(#\d{1})";
string input = "111111#1";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
var firstGroupValue = match.Groups[1]; // "111111"
var secondGroupValue = match.Groups[2]; // "#1"
}
- 2 回答
- 0 關注
- 218 瀏覽
添加回答
舉報
0/150
提交
取消