Search code examples
c++regexboostboost-xpressive

Using Boost::Xpressive to match a single character


I have a string that can be "/" "+" "." or a descriptive name

I'm trying to figure out how to use regex to check if the string matches any of the 3 special characters above (/ + or .)

After doing a bit of reading i decided boost::xpressive was the way to go but i still cannot figure it out.

is Boost:xpressive suitable for this task and what would my regex string need to be?

thanks


Solution

  • Why not just use std::string::find_first_of() to do your own solution? Sounds like a lot of machinery for a fairly simple task.

    Edit

    Try this out if you're still stuck.

    #include <iostream>
    #include <boost/xpressive/xpressive.hpp>
    
    using namespace std;
    using namespace boost::xpressive;
    
    int main()
    {
       sregex re = sregex::compile("[+./]|[:word:]+");
    
       sregex op = as_xpr('+') | '.' | '/';
       sregex rex = op | (+alpha);
    
       if (regex_match(string("word"), re))
          cout << "word" << endl;
       if (regex_match(string("word2"), re))
          cout << "word2" << endl;
       if (regex_match(string("+"), re))
          cout << "+" << endl;
       return 0;
    }
    

    There are two ways to do the same thing shown. The variable named re is intialized with a perl-like regular expression string. rex uses Xpressive native elements.