regex - C# Regular Expression - Extracting the number of month or year from codes -


i 1 regular expression extract number of days, weeks, months and/or years following codes:

ab7yt1m=abc       ==> 7y1m  ab10yt1m=abc      ==> 10y1m  ab30yt1m=abc      ==> 30y1m  abcdef1y1m=a      ==> 1y1m  abcdef34y6m=a     ==> 34y6m  abcdef7m=a        ==> 7m  abcdef1d=a      ==> 1d   @"(\d+[dwmy])(?!\w+(1))(\d+[dwmy])(?!\w+(1))|(\d+[dwmy])(?!\w+(1))" 

this code not support e.g. 30yt1m

could please find appropriate regexp me?

this expression trick (demo):

(?:(\d+)y)?t?(?:(\d+)m) 

in order data need, content of first , second capturing groups. when corresponding entry present in string, first group contain year, , second contain month (demo):

var data = new[] {     "ab7yt1m=abc",     "ab10yt1m=abc",     "ab30yt1m=abc",     "abcdef1y1m=a",     "abcdef34y6m=a",     "abcdef7m=a" }; regex r = new regex(@"(?:(\d+)y)?t?(?:(\d+)m)"); foreach (var s in data) {     var m = r.match(s);     if (m.success) {         var yy = m.groups[1];         var mm = m.groups[2];         console.writeline("y='{0}', m='{1}'", yy, mm);     } } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -