Getting my head round Regex
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Getting my head round Regex

Hybrid View

  1. #1
    Join Date
    Nov 2012
    Posts
    4

    Getting my head round Regex

    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.

  2. #2
    Join Date
    Nov 2012
    Posts
    4

    Re: Getting my head round Regex

    I cannot modify the first post, but in the list of format(s) to search for, there should also have been

    [Disc1of2]

  3. #3
    Join Date
    Aug 2008
    Posts
    73

    Re: Getting my head round Regex

    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.

  4. #4
    Join Date
    Nov 2012
    Posts
    4

    Re: Getting my head round Regex

    Addendum,

    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).

  5. #5
    Join Date
    Nov 2012
    Posts
    4

    Re: Getting my head round Regex

    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.

    ie.

    Baseball 2002 (Disk 1 of 2) (Europe)
    Baseball 2002 (E) (1/2)
    Baseball 2002 [disc1of2]
    Baseball 2002 (Disc 17)

    etc.

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center