Looking for a specific regex
-
Hey all, I am a bit disappointed by myself to come up with that question, but hey! there are some regex cracks here that can easily answer this question and maybe deepen my understanding. I really tried to figure out the best regex for my problem - but I am new to programming and regex' still seem to make my head explode. Here's the string I've got:
Directory\Subdirectory\Subdir\Subsubdir\sub_sub_subv009\filename_with_lots_of_ABC123.txt
using this regex:
(?<=_)[a-zA-z0-9]*(?=.txt$)
I get:
of_ABC123
Close call, but what I need is just ABC123 - this are all characters as a string between the LAST '_' and ".txt" Additionally, I did this in Expresso (did not use the designer but did a lot of test runs to see if my regex matches) - will this be the regex to be used in C#? I really appreciate any hint to solve my last problem, Best Dennis
-
Hey all, I am a bit disappointed by myself to come up with that question, but hey! there are some regex cracks here that can easily answer this question and maybe deepen my understanding. I really tried to figure out the best regex for my problem - but I am new to programming and regex' still seem to make my head explode. Here's the string I've got:
Directory\Subdirectory\Subdir\Subsubdir\sub_sub_subv009\filename_with_lots_of_ABC123.txt
using this regex:
(?<=_)[a-zA-z0-9]*(?=.txt$)
I get:
of_ABC123
Close call, but what I need is just ABC123 - this are all characters as a string between the LAST '_' and ".txt" Additionally, I did this in Expresso (did not use the designer but did a lot of test runs to see if my regex matches) - will this be the regex to be used in C#? I really appreciate any hint to solve my last problem, Best Dennis
First of all, if you have a regex question, there is a regular expression forum that should be used. Secondly, not every string manipulation requires a regex - a much simpler way would be to use
string.LastIndexOf
for the_
and IndexOf the.
character and then just extract the substring from it.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Hey all, I am a bit disappointed by myself to come up with that question, but hey! there are some regex cracks here that can easily answer this question and maybe deepen my understanding. I really tried to figure out the best regex for my problem - but I am new to programming and regex' still seem to make my head explode. Here's the string I've got:
Directory\Subdirectory\Subdir\Subsubdir\sub_sub_subv009\filename_with_lots_of_ABC123.txt
using this regex:
(?<=_)[a-zA-z0-9]*(?=.txt$)
I get:
of_ABC123
Close call, but what I need is just ABC123 - this are all characters as a string between the LAST '_' and ".txt" Additionally, I did this in Expresso (did not use the designer but did a lot of test runs to see if my regex matches) - will this be the regex to be used in C#? I really appreciate any hint to solve my last problem, Best Dennis
Here is an example of solution using both Regex and string class methods. In Regex I use a group with "FileCode" name to get result. As Pete said not all cases require using of Regex. And in this case I think would be better to use string methods (alternative variant).
const string pattern = @"\_(?(\[^\_\]\[a-zA-Z0-9\])+)\\.txt$"; const string text = @"Directory\\Subdirectory\\Subdir\\Subsubdir\\sub\_sub\_subv009\\filename\_with\_lots\_of\_ABC123.txt"; var groupFileCode = Regex.Match(text, pattern); Console.WriteLine(groupFileCode.Groups\["FileCode"\].Value); // alternatively without regullar expressions int lastUnderscoreIndex = text.LastIndexOf('\_'); int lastPointIndex = text.LastIndexOf('.'); Console.WriteLine(text.Substring(lastUnderscoreIndex + 1, lastPointIndex - lastUnderscoreIndex - 1));
Regards, savbace
-
Hey all, I am a bit disappointed by myself to come up with that question, but hey! there are some regex cracks here that can easily answer this question and maybe deepen my understanding. I really tried to figure out the best regex for my problem - but I am new to programming and regex' still seem to make my head explode. Here's the string I've got:
Directory\Subdirectory\Subdir\Subsubdir\sub_sub_subv009\filename_with_lots_of_ABC123.txt
using this regex:
(?<=_)[a-zA-z0-9]*(?=.txt$)
I get:
of_ABC123
Close call, but what I need is just ABC123 - this are all characters as a string between the LAST '_' and ".txt" Additionally, I did this in Expresso (did not use the designer but did a lot of test runs to see if my regex matches) - will this be the regex to be used in C#? I really appreciate any hint to solve my last problem, Best Dennis
-
Your range is [a-zA-z0-9] - notice the second "z" is lower case, and as "_" falls between uppercase A and lowercase z it is included in your match.
Thank you so much! I have overseen this. So I have at last understood a little bit of regex and did it right. Thank you also for the other answers, especially the C# code examples. I will try them tomorrow!
-
Hey all, I am a bit disappointed by myself to come up with that question, but hey! there are some regex cracks here that can easily answer this question and maybe deepen my understanding. I really tried to figure out the best regex for my problem - but I am new to programming and regex' still seem to make my head explode. Here's the string I've got:
Directory\Subdirectory\Subdir\Subsubdir\sub_sub_subv009\filename_with_lots_of_ABC123.txt
using this regex:
(?<=_)[a-zA-z0-9]*(?=.txt$)
I get:
of_ABC123
Close call, but what I need is just ABC123 - this are all characters as a string between the LAST '_' and ".txt" Additionally, I did this in Expresso (did not use the designer but did a lot of test runs to see if my regex matches) - will this be the regex to be used in C#? I really appreciate any hint to solve my last problem, Best Dennis
-
Hey all, I am a bit disappointed by myself to come up with that question, but hey! there are some regex cracks here that can easily answer this question and maybe deepen my understanding. I really tried to figure out the best regex for my problem - but I am new to programming and regex' still seem to make my head explode. Here's the string I've got:
Directory\Subdirectory\Subdir\Subsubdir\sub_sub_subv009\filename_with_lots_of_ABC123.txt
using this regex:
(?<=_)[a-zA-z0-9]*(?=.txt$)
I get:
of_ABC123
Close call, but what I need is just ABC123 - this are all characters as a string between the LAST '_' and ".txt" Additionally, I did this in Expresso (did not use the designer but did a lot of test runs to see if my regex matches) - will this be the regex to be used in C#? I really appreciate any hint to solve my last problem, Best Dennis
I'd likely use
System.IO.Path.GetFileNameWithoutExtension
to narrow it down first, then use LastIndexOf and Substring as others have suggested. Were I to use a Regex, I'd likely use_(?'Name'[^_\.]+)\.
I prefer to use named groups to capture what I want. You may want to look at the RightToLeft option as well; very handy for getting stuff that follows other stuff. Oh, and I use this handy tool: RegexTester[^] :-D