Reputation: 3558
I need some help writing a regex. I have the following strings,
xxx.yyy.wwwwwaaa_IN_123
xxx.rrrttttt_IN_12355
zz.iiiiolll_IN_12
xxx.zzzz.rrrr.yyy.wwwwwwww_IN_1232
Using Regex.Replace, I want to change the string from the above format to something like
"$1($2)"
where $2
would be the number at the end of the string and $1
would be the first three letters of the last substring before the _IN_
mark.
In another words,
xxx.yyy.wwwwwaaa_IN_123 www(123)
xxx.rrrttttt_IN_12355 rrr(12355)
iiiiolll_IN_12 iii(12)
xxx.zzzz.rrrr.yyy.wwwwwwww_IN_1232 www(1232)
This is what i have,
".*.([^\.]{3})[^\.]_IN_+([0-9]+)"
but this only takes the last letters before the _IN_
mark, and not the first letters of the last substring.
Thanks in advance
Upvotes: 0
Views: 193
Reputation: 4554
Well, the right regex is:
Regex r = new Regex("([a-z]{3})[a-z]*_IN_(\d+)");
You might need a RegexOptions.IgnoreCase in case there might be upper case letters. If you the regex is defined as a static member you might consider using RegexOptions.Compiled.
The above Regex will match bbb(123) in the string aaa_bbbbbb_IN_123.
The answer made by L.B would match aaa.
The answer made by Frederik C will not match because there is no "." (like your 3rd example)
Upvotes: 0
Reputation: 660
This does the trick, the non-greedy parts makes it not capture to much...
Regex.Match(input, @"(?:.*?\.)?(.{3})[^.]*?_IN_(\d+)");
Upvotes: 2