正则表达式:仅限A-Z字符

时间:2012-08-05 10:55:22

标签: c# regex

大家好我有一个字符串例如...

“Afds 1.2 45002”

我想用正则表达式做的是从左边开始,返回字符A-Z || a-z直到遇到未匹配的。

所以在上面的例子中我想要返回“Afds”。

另一个例子

“BCAD 2.11 45099 GHJ”

在这种情况下,我只想要“BCAD”。

由于

3 个答案:

答案 0 :(得分:5)

您想要的表达式是:/^([A-Za-z]+)/

答案 1 :(得分:5)

使用此正则表达式(?i)^[a-z]+

Match match = Regex.Match(stringInput, @"(?i)^[a-z]+");

(?i) - 忽略大小写

^ - 字符串

的开头

[a-z] - 任何拉丁字母

[a-z ] - 任何拉丁字母或空格

+ - 一个或多个previos符号

答案 2 :(得分:0)

string sInput = "Afds 1.2 45002";

Match match = Regex.Match(sInput, @"^[A-Za-z]+",
              RegexOptions.None);

// Here we check the Match instance.
if (match.Success)
{
    // Finally, we get the Group value and display it.
    string key = match.Groups[1].Value;
    Console.WriteLine(key);
}
相关问题