[英]Match regular expression for the pattern dddG-xyz
My try: 我的尝试:
string exp1 = "\\d+G-";
string z = Regex.Match("CN=314G-VK1,OU=Grupper,OU=314,OU=Skole,OU=03Skien,DC=login,DC=sk-asp,DC=no",exp1).Value;
Console.WriteLine(z);
I want to match 314G-VK1 from the string z . 我想从字符串z中匹配314G-VK1 。
Where, 哪里,
314 is decimal and it can be any number of digit. 314是十进制,可以是任意数字。 say 125632588 .
说125632588 。
G- is constant. G-是恒定的。
And VK1 can be character or decimal but length will be only 3, say er5 . VK1可以是字符或十进制,但长度仅为3,例如er5 。
How can i meet the requirements? 我如何满足要求?
From my code i only get output 314G- . 从我的代码中,我只会得到输出314G- 。 I tried several ways but that don't help me anymore.
我尝试了几种方法,但这对我没有帮助。
You may use 您可以使用
string exp1 = @"\d+G-\w{3}";
See the regex demo 见正则表达式演示
The \\w{3}
pattern will match 3 word chars, ie mostly letters, digits, underscores. \\w{3}
模式将匹配3个字符,即大部分为字母,数字和下划线。 You may precise it if need be, eg to only match 3 uppercase ASCII letters or digits, you may use [A-Z0-9]{3}
. 您可以根据需要进行精确调整,例如仅匹配3个大写ASCII字母或数字,则可以使用
[A-Z0-9]{3}
。 To also include lowercase letters, add them to the character class, [A-Za-z0-9]{3}
. 要同时包含小写字母,请将它们添加到字符类
[A-Za-z0-9]{3}
。
Regulex regex graph : Regulex正则表达式图 :
.NET regex test results: .NET正则表达式测试结果:
C# code demo : C#代码演示 :
string exp1 = @"\d+G-\w{3}";
string s = "CN=314G-VK1,OU=Grupper,OU=314,OU=Skole,OU=03Skien,DC=login,DC=sk-asp,DC=no";
string z = Regex.Match(s, exp1)?.Value;
Console.WriteLine(z); // => 314G-VK1
You are almost correct but, you just need to add [0-9a-zA-Z]{3}
after G- . 您几乎是正确的,但是,您只需要在G-之后添加
[0-9a-zA-Z]{3}
。
string exp1 = "\\d+G-[0-9a-zA-Z]{3}";
string z = Regex.Match("CN=314G-VK1,OU=Grupper,OU=314,OU=Skole,OU=03Skien,DC=login,DC=sk-asp,DC=no", exp1).Value;
Console.WriteLine(z);
Try this: string exp1 = @"\\d+G-[a-zA-Z0-9]{3}" 试试这个:字符串exp1 = @“ \\ d + G- [a-zA-Z0-9] {3}”
[a-zA-Z0-9]{3} will match with 3-char alphanumeric string. [a-zA-Z0-9] {3}将与3个字符的字母数字字符串匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.