RegEx Query / C# Fix RRS feed

  • Question

  • Hi,

    I have the data as follows


    I need only to get the format "ABC-XXX-XXX". TheRegEx pattern I am using is "[A-Z]{3}-[0-9]{4}-[0-9]{3,10}".

    Plus there is a method written in C#

        public String RemoveChars(String message)
            // The pattern for an expression
            String charPattern = "[A-Z]{3}-[0-9]{4}-[0-9]{3,10}";
            // Create a regex object with the pattern
            Regex objRegExp = new Regex(charPattern);
            // Replace unwanted char by empty string
            message = objRegExp.Replace(message, String.Empty);
            // Return the message without unwanted chars
            return message;

    So if I run this code now, for "CVE-2011-0573," it returns "," and not "CVE-2011-0573".

    Could you please help me to fix that C code so it works the way I need?

    Thanks a lot!

    Friday, February 21, 2014 12:20 PM

All replies

  • If I understand your objective correctly, you want the Match member of your Regex, not the Replace. Replace does just what it says: replace the matched text with the given string.  It matches "CVE-2011-0573" and therefore replaces it with string.Empty, leaving you with only the text that didn't match--the comma.

    I don't recall the exact syntax off the top of my head, but you want something like this:

    Match match = objRegExp.Match(message);
    if (match.Success)
        message = match.Value; //I'm unsure of this syntax
        ; //The input message doesn't have the expected text

    Wednesday, March 5, 2014 3:27 PM