【发布时间】:2019-12-24 10:36:48
【问题描述】:
我有以下字符串
- mo474334pt1572
- at1088ma15
- ma15pt1983
- ca1
我要获得
a) string[] result = new[] {"mo474334", "pt1572"};
a) string[] result = new[] {"at1088", "ma15"};
a) string[] result = new[] {"ma15", "pt1983"};
c) string[] result = new[] {"ca1"};
我一直在尝试以下方法,但收效甚微
string[] result = Regex.Split(str, @"\[A-Za-z]{2}[0-9]*");
string[] result = Regex.Split(str, @"\[A-Za-z]{2}[0-9]*\[A-Za-z]{2}[0-9]*");
和
string[] result = Regex.Matches(str, @"^[A-Za-z]{2}[0-9]*").Cast<Match>().Select(m => m.Value).ToArray();
谁能看到我哪里出错了,这可以实现吗?或者我应该使用其他方法
【问题讨论】:
-
您可以在边界数字/字母上拆分:
(?<=\d)(?=[a-z]