简体   繁体   English

正则表达式:仅限AZ字符

[英]Regex: A-Z characters only

Hi guys I have a string for example... 嗨,大家好,我有一个字符串...

"Afds 1.2 45002" “ Afds 1.2 45002”

What I wanted to do using regex is to start from the left, return characters AZ || 我想使用正则表达式从左开始,返回字符AZ ||。 az until an unmatached is encountered. 直到遇到未固定状态为止。

So in the example above I wanted to return "Afds". 因此,在上面的示例中,我想返回“ Afds”。

Another example 另一个例子

"BCAD 2.11 45099 GHJ" “ BCAD 2.11 45099 GHJ”

In this case I just want "BCAD". 在这种情况下,我只需要“ BCAD”。

Thanks 谢谢

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

use this regular expression (?i)^[az]+ 使用此正则表达式(?i)^[az]+

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

(?i) - ignore case (?i) -忽略大小写

^ - begin of string ^ -字符串的开头

[az] - any latin letter [az] -任何拉丁字母

[az ] - any latin letter or space [az ] -任何拉丁字母或空格

+ - 1 or more previos symbol + -1个或更多previos符号

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM