I have a string of the form:
我有一串表格:
codename123
Is there a regular expression that can be used with Regex.Split()
to split the alphabetic part and the numeric part into a two-element string array?
是否有正则表达式可以与Regex.Split()一起使用,将字母部分和数字部分分割为两个元素字符串数组?
7 个解决方案
#1
43
I know you asked for the Split
method, but as an alternative you could use named capturing groups:
我知道你想要拆分方法,但作为一种选择,你可以使用命名捕获组:
var numAlpha = new Regex("(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)");
var match = numAlpha.Match("codename123");
var alpha = match.Groups["Alpha"].Value;
var num = match.Groups["Numeric"].Value;
#2
8
splitArray = Regex.Split("codename123", @"(?<=\p{L})(?=\p{N})");
will split between a Unicode letter and a Unicode digit.
将在Unicode字母和Unicode数字之间进行分割。
#3
6
Regex is a little heavy handed for this, if your string is always of that form. You could use
如果您的字符串始终是那种形式,那么Regex就有点笨拙了。您可以使用
"codename123".IndexOfAny(new char[] {'1','2','3','4','5','6','7','8','9','0'})
and two calls to Substring.
两个调用子字符串。
#4
4
A little verbose, but
有点冗长,但
Regex.Split( "codename123", @"(?<=[a-zA-Z])(?=\d)" );
Can you be more specific about your requirements? Maybe a few other input examples.
你能更具体地说明你的要求吗?也许还有其他一些输入例子。
#5
4
IMO, it would be a lot easier to find matches, like:
在我看来,找到比赛要容易得多,比如:
Regex.Matches("codename123", @"[a-zA-Z]+|\d+")
.Cast<Match>()
.Select(m => m.Value)
.ToArray();
rather than to use Regex.Split
.
而不是使用Regex.Split。
#6
1
Another simpler way is
另一个简单的方法是
string originalstring = "codename123";
string alphabets = string.empty;
string numbers = string.empty;
foreach (char item in mainstring)
{
if (Char.IsLetter(item))
alphabets += item;
if (Char.IsNumber(item))
numbers += item;
}
#7
1
Well, is a one-line only: Regex.Split("codename123", "^([a-z]+)");
只有一行:Regex。分割(“codename123”、“^”([a - z]+));
#1
43
I know you asked for the Split
method, but as an alternative you could use named capturing groups:
我知道你想要拆分方法,但作为一种选择,你可以使用命名捕获组:
var numAlpha = new Regex("(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)");
var match = numAlpha.Match("codename123");
var alpha = match.Groups["Alpha"].Value;
var num = match.Groups["Numeric"].Value;
#2
8
splitArray = Regex.Split("codename123", @"(?<=\p{L})(?=\p{N})");
will split between a Unicode letter and a Unicode digit.
将在Unicode字母和Unicode数字之间进行分割。
#3
6
Regex is a little heavy handed for this, if your string is always of that form. You could use
如果您的字符串始终是那种形式,那么Regex就有点笨拙了。您可以使用
"codename123".IndexOfAny(new char[] {'1','2','3','4','5','6','7','8','9','0'})
and two calls to Substring.
两个调用子字符串。
#4
4
A little verbose, but
有点冗长,但
Regex.Split( "codename123", @"(?<=[a-zA-Z])(?=\d)" );
Can you be more specific about your requirements? Maybe a few other input examples.
你能更具体地说明你的要求吗?也许还有其他一些输入例子。
#5
4
IMO, it would be a lot easier to find matches, like:
在我看来,找到比赛要容易得多,比如:
Regex.Matches("codename123", @"[a-zA-Z]+|\d+")
.Cast<Match>()
.Select(m => m.Value)
.ToArray();
rather than to use Regex.Split
.
而不是使用Regex.Split。
#6
1
Another simpler way is
另一个简单的方法是
string originalstring = "codename123";
string alphabets = string.empty;
string numbers = string.empty;
foreach (char item in mainstring)
{
if (Char.IsLetter(item))
alphabets += item;
if (Char.IsNumber(item))
numbers += item;
}
#7
1
Well, is a one-line only: Regex.Split("codename123", "^([a-z]+)");
只有一行:Regex。分割(“codename123”、“^”([a - z]+));