如果我有一个字符串,我该如何从该字符串中提取第一个字母字符,以便以起始字母字符或空字符串结束。例如:
AB12 9HG => "AB"
PJ11 => "PJ"
123JO => ""
这可以用正则表达式实现还是有更简单的方法?
最佳答案
这是一个例子(https://dotnetfiddle.net/CiIaqx):
var inputs = new [] { "ASD123", "PJ11", "123JO" };
foreach (var input in inputs)
{
var match = Regex.Match(input, "^([a-z]*)", RegexOptions.IgnoreCase);
if (match.Success)
{
Console.WriteLine("Checking: " + input + ", found: " + match.Groups[1].Value);
}
}
这个输出:
Checking: ASD123, found: ASD
Checking: PJ11, found: PJ
Checking: 123JO, found:
关于c# - 如何从字符串中获取第一组字母字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39292012/