c# - Why does this Regular Expression not return the correct match? -


i trying return part of string, want return before fist slash:

ec22941c/02/ori 

should give me: ec22941c

i have used http://www.regexr.com/ build expression:

(ec.+?)\/.+ 

when tested against text:

ec22941c/02/ori 

it correctly tells me first group is

ec22941c 

when put c#:

 public static string getinstructionref(string supplierreferenceid)     {         // instruciton ref bit before slash                     var match = regex.match(supplierreferenceid, @"(ec.+?)\/.+");          if (match == null || match.groups.count == 0)             return "";          // return first group instruction ref         return match.groups[0].value;     } 

the result is:

ec22941c/02/ori 

i have tried number of different patterns , seem same thing.

does have idea im doing wrong?

the issue you're returning wrong group index, 0 return entire match while 1 returns matched context of capturing parentheses — numbered left right.

return match.groups[1].value; 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -