none
need assistance on regex for entire string RRS feed

  • Question

  • Hello,

    I am attempting to apply a regex to the following string:

    1Chapter I"DISCOVER A COMMON HATE

    with this regular expression (in native C++):

    {(([0-9]+(.?))|((\bChapter\b)+)|((\b I \b)+))}

    I would like to match 1, Chapter, and the letter I.

    As of now, the first match is 1C and not just 1.

    Thanks.


    williamj

    Wednesday, December 18, 2013 4:31 PM

Answers

  • Try this code.  I tested in C# but should work in C++

               string input = "123 Chapter I DISCOVER A COMMON HATE";
                string pattern = @"(?'number'\d+)\s+Chapter\s+(?'letter'[^\s]+)";
    
                Regex expr = new Regex(pattern, RegexOptions.None);
           
                Match match = expr.Match(input);
                Console.WriteLine("Chapter Number : {0} Chapter Letter : {1}", match.Groups["number"].Value, match.Groups["letter"].Value);


    jdweng

    • Marked as answer by williamj8 Wednesday, December 18, 2013 8:52 PM
    Wednesday, December 18, 2013 8:42 PM

All replies

  • Try pattern below

    string pattern = @"^(?'number'\n*)\b*Chapter\b*(?'letter'[^b]*)"


    jdweng

    Wednesday, December 18, 2013 5:03 PM
  • Hello,

    Thanks for your reply.

    Could you be more specific for the number and Chapter sections?

    Again, the app is native C++.

    The regex match is still returning, 1C (and not just 1) for 1Chapter string.

    Thanks.


    williamj

    Wednesday, December 18, 2013 5:14 PM
  • Try this code.  I tested in C# but should work in C++

               string input = "123 Chapter I DISCOVER A COMMON HATE";
                string pattern = @"(?'number'\d+)\s+Chapter\s+(?'letter'[^\s]+)";
    
                Regex expr = new Regex(pattern, RegexOptions.None);
           
                Match match = expr.Match(input);
                Console.WriteLine("Chapter Number : {0} Chapter Letter : {1}", match.Groups["number"].Value, match.Groups["letter"].Value);


    jdweng

    • Marked as answer by williamj8 Wednesday, December 18, 2013 8:52 PM
    Wednesday, December 18, 2013 8:42 PM
  • Hi,

    My application is a native C++ app.

    I was able to match a number with a decimal.

    I used this regex:

    ([0-9\\.]+

    Thanks.


    williamj

    Wednesday, December 18, 2013 8:52 PM
  • Hello,

    Here is my corrected regex:

    ([0-9]+\\.?)

    Thanks.


    williamj

    Wednesday, December 18, 2013 9:01 PM