C#正则表达式获取字符串甜菜根数



我有一个看起来像的简单字符串

string text = "1t2:3|5"

所以我的目标是得到数字之间的字符,比如:

string[] result = {"t", ":", "|"}

我试图通过先获取数字来解决这个问题

Regex.Matches(text , "(-?[0-9]+)").OfType<Match>().Select(m => int.Parse(m.Value)).ToArray();

然后使用这个正则表达式模式

/[^\w]/g

我得到了符号,但"\t〃;被拆分为"并且没有";t〃;,如何正则表达式?

你可以用RegexString.Split来做——结果是相同的

char[] separators = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };        
var splitResult = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);        
var matchesResult = Regex.Matches(text, @"(?<=d)D+(?=d)").Select(w => w.Value).ToArray();

https://dotnetfiddle.net/csj1f6

最新更新