Boost RegEx

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rottmanj
    New Member
    • Jan 2008
    • 46

    Boost RegEx

    In my application I get a string from my datasource that I need to replace some of the bad chars in the strings.

    Right now I have written a pretty standard regex that should replace any of the bad chars in the strings. However, instead of removing the bad chars it removes all chars from the string.

    The bad chars I am trying to replace are /'()

    Here is the current code that I am testing with.

    Code:
    boost::regex const string_matcher("/'()//gi");
    dataStr = boost::regex_match(results->GetString(column),string_matcher);
    cout << dataStr << endl;
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    The algorithm regex_match determines whether a given regular expression matches all of a given character sequence denoted by a pair of bidirectional-iterators.

    You want to replace characters so why are you using regex_match?
    Why aren't you using the regex_replace method?

    Comment

    • rottmanj
      New Member
      • Jan 2008
      • 46

      #3
      I do believe I read the api wrong then.

      However, I still have an issue with this.

      When i use the pattern below, I do not get the desired result of replacing . with _ and ' with \. Using the code below does not replace any of the bad chars.

      If I have the string ROOM.width and I want to replace . with _, what would the syntax be for my regex.

      Currently this is the code that I am testing with.

      Code:
      boost::regex const pattern("//'.()/gi", boost::regex_constants::icase|boost::regex_constants::perl);
      string replace("_");
      cout << boost::regex_replace (column, pattern, replace) << endl;

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Some of the characters you are attempting to locate are regex syntax.

        Please look into how to create regex expressions. One of my favorite resources on the topic is this site.

        Your regex should look something like:

        [\.\'\(\)]


        Please take a look at the resource that I linked you to to understand how I came up with the expression.

        A great place to check your regexs is this site.

        -Frinny

        Comment

        Working...