c++ - Regex to match either one of two filename patterns -


i trying match filenames using boost::regex , have 2 kinds of patters:

  1. xyzsomestring
  2. xysomestringending

the string somestring can (>0 characters). beginning of filename either xyz or xy. if xy , there has string ending terminates whole sequence. tried combine 2 regex | doesnt work. matches filenames first pattern:

(xyz)(.*) 

and matches filenames second pattern:

(xy)(.*)(ending) 

but when combine them, first pattern matches:

((xyz)(.*))|((xy)(.*)(ending)) 

all supposed case insensitive, why use boost::regex::icase in constructor. have tried without icase, doesnt work either).

any suggestions?

there may simpler expressions think regex ^xy(?(?!z).*ending$|.*$) should it:

#include <iostream> #include <string> #include <boost/regex.hpp>  bool bmatch(const std::string& x, const std::string& re_) {   const boost::regex re(re_, boost::regex::icase);   boost::smatch what;   return boost::regex_match(x, what, re); }  int main() {   std::string re = "^xy(?(?!z).*ending$|.*$)";   std::vector<std::string> vx = { "xyz124f5sf", "xyz12345",                                   "xy38fsj dfending", "xy4 dfhd ending",                                   "xyz", "xy345kendi", "xy56nding" };   (auto : vx) {     std::cout << "\nstring '" << i;     if (bmatch(i, re)) {       std::cout <<         "' matched." << std::endl;      } else {       std::cout <<         "' not matched." << std::endl;      }   }    return 0; } 

here's live demo.

edit: additionally, think regex ^xy(z.*|.*ending)$ should work well.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -