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 .
Where,
314 is decimal and it can be any number of digit. say 125632588 .
G- is constant.
And VK1 can be character or decimal but length will be only 3, say er5 .
How can i meet the requirements?
From my code i only get output 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. You may precise it if need be, eg to only match 3 uppercase ASCII letters or digits, you may use [A-Z0-9]{3}
. To also include lowercase letters, add them to the character class, [A-Za-z0-9]{3}
.
.NET regex test results:
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- .
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);
Check demo here
Try this: string exp1 = @"\\d+G-[a-zA-Z0-9]{3}"
[a-zA-Z0-9]{3} will match with 3-char alphanumeric string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.