c# - Regex to parse Active Directory string fails -


i have block of code in c# code-behind:

string input = "cn=l_wdjack127_wdc_ssis_user_ch,ou=alosup,ou=infra,dc=internal, dc=mycompany,dc=com" string pattern = @"cn\=(.+)\,";  matchcollection matches = regex.matches(input, pattern); foreach (match match in matches) {     console.writeline(match.groups[1].value); } 

when run this, match.groups[1].value equal

l_wdjack127_wdc_ssis_user_ch,ou=alosup,ou=infra,dc=internal, dc=mycompany

i need equal

l_wdjack127_wdc_ssis_user_ch

can please fix regex?

basic greedy/lazy quantifier problem:

string pattern = @"cn\=(.+?)\,"; 

this resource should why: http://www.regular-expressions.info/repeat.html

basically, .+ tries match as many of character, , @ least 1 of which, possible before hitting last comma. adding ? end of (.+?) tell regex engine match as many of any, , @ least 1 of which, character possible before hit first comma.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -