I am hopeful there is a Regex guru who can help me with this particular problem. I have not had a great amount of experience using regex, though am mostly fine with the simpler elements.
What I am trying to do is strip a certain element from a string that contains a full filename with bracketed data. The data I need to check for is in the format(s) of,
(Disc 1 of 2)
(disc 1)
(1/2)
(disc 1/2)
and variations of. These are unique and only one instance is present per string (or none)
I need to locate these and extract the first value (numerical), a '1' in the cases posted. This value I want to use to create a uniform format of the myrad of formats shown above.
The second number is not needed as I only want to create a string of the format
(Disc x)
Any help would be wonderful as I have been struggling with this for some time.
Following code should extract first digit from your string
Code:
Match m;
string someString = "(Disc 1 of 2)";
Regex r = new Regex(@"\d");
m = r.Match(someString);
string firstNumberAsString = m.Groups[0].ToString(); //You can then convert it into an integer.
Last edited by ricky_cpp; November 19th, 2012 at 07:38 AM.
Though, also noting that not all bracketted areas are related to disc numbers. Some may contain revisions, regions, or other data. So, just trying to find a match pattern that works with the examples in the first post really (without grabbing non-matches).
Thanks, but this will just find the first digit in the filename. I mentioned that I wanted to strip a certain element from a string that contains a full filename with bracketed data. I should really have given a better description rather than just the bracketed data I wished to match/search.
So, I need to grab the braketed area (sometimes in square brackets) and then take the first digit(s). This is where my knowledge comes completely unstuck. I am sure there is an elegent way to do this. Well... mostly sure.. lol
Bookmarks