我需要某種方法,使用正則表達式或拆分我不知道以下內容。從定義的分隔符到字符串數組之間的字符串提取所有子字符串
我有串,看起來像這樣:
ls 0
[0,86,180]
ls 1
[1,2,200]
ls 2
[2,3,180]
ls 3
[3,4,234]
...等等。我希望括號[]之間的所有內容都是字符串數組中的一個字符串,其他所有內容都可以忽略。
我需要某種方法,使用正則表達式或拆分我不知道以下內容。從定義的分隔符到字符串數組之間的字符串提取所有子字符串
我有串,看起來像這樣:
ls 0
[0,86,180]
ls 1
[1,2,200]
ls 2
[2,3,180]
ls 3
[3,4,234]
...等等。我希望括號[]之間的所有內容都是字符串數組中的一個字符串,其他所有內容都可以忽略。
無法識別的轉義序列時試圖創建正則表達式 – user1480742
這可能給你的整體思路和步驟:
var yourString = @"ls 0
[0,86,180]
ls 1
[1,2,200]
ls 2
[2,3,180]
ls 3
[3,4,234]";
var result = yourString.Split(new char[] { '\n' }) //split
.Where(i => i.Contains('[')) //filter
.Select(i => i.Replace("[", string.Empty) //prepare
.Replace("]", string.Empty))
.ToList();
var newArray = string.Join(",", result); //merge the result
如果塊數並不總是套三分球
var myString = "ls 0
[0,86,180]
ls 1
[1,2,200]
ls 2
[2,3,180]
ls 3
[3,4,234]";
RegEx pattern = new RegEx(@"\[[\d,]*\]*");
Match numbers = pattern.Match(myString);
if(numbers.Success)
{
do {
var val = numbers.Value;
// code for each block of numbers
numbers.NextMatch();
} while(numbers.Success)
}
的[我如何提取文本謊言
可能重複在圓括號(圓括號)之間?](http://stackoverflow.com/questions/378415/how-do-i-extract-text-that-lies-between-parentheses-round-brackets) –