我想匹配用逗號分隔的任何三個數字,並且可以用括號開頭和結尾。使用正則表達式從文本中拆分三個整數c#
有效實例是: {30,40,50},[30,40,50],(30,40,50)或30,40,50
它可能也30,40,50 (裝置,可以逗號之後是空間和三個數字中的一個之前)
非有效的例子是: {30,40,50或30,40,50 {[
和後我想只拆分整合。
我想:
Regex regex = new Regex(@"({|(|[)?\d+,\d+,\d+({|(|[)?");
Match match = regex.Match(laticeDim.Text);
if (match.Success)
{
int[] integers = laticeDim.Text.Trim('{', '}', '[', ']', '(', ')').Split(',')
.Select(s => Convert.ToInt32(s))
.ToArray();
}
但我得到了第一行代碼運行時錯誤之上。
有什麼建議嗎?匹配這種表達的最佳方式是什麼?
Try ['{(? \ d +(?:, \ d +){2})} | \ [(? \ d +(?:, \ d +){2})] | \((? \(?!, \ d +){2})\)|(?<![[({])\ b(? \ d +(?:, \ d +){2})\ ])}])'](HTTP://regexstorm.net/tester p =%7B \(%3F%3cval%3E%5CD%2B \(%3F%3A%2C%5CD%2B \)%7B2 %7D \)%7D%7C%5C%5B \(%3F%3cval%3E%5CD%2B \(%3F%3A%2C%5CD%2B \)%7B2%7D \)%5D%7C%5C \(\(%3F%3cval%3E%5CD%2B \(%3F%3A%2C%5CD%2B \)%7B2%7D \)%5C \)%7C \(%3F%3C!%5B%5B \(%7B%5D \)%5CB \(%3F%3cval%3E%5CD%2B \(%3F%3A%2C%5CD%2B \)%7B2%7D \)%5CB \( %3F!%5B%5D \)%7D%5D \)I = +%7b30%2C40%2c50%7D +%5b30%2C40%2c50%5D + \(30%2C40%2c50 \)++ 30%2C40%2c50 %0d%0a%7b30%2c40%2c50 +或+%7b30%2c40%2c50%5d)。使用'match.Groups [「val」]。值' –