6 #include <boost/regex.hpp>
13 // Escape special chars.
14 // All characters are literals except: '.|*?+(){}[]^$\'
15 // These characters are literals when preceded by a "\", which is done here
16 // This function is unfortunately copied from ../frontend_helpers.cpp, so we should
17 // try to make sure to keep the two in sync.
18 string const escape_special_chars(string const & expr)
20 // Search for all chars '.|*?+(){}[^$]\'
21 // Note that '[' and '\' must be escaped.
22 // This is a limitation of boost::regex, but all other chars in BREs
23 // are assumed literal.
24 boost::regex reg("[].|*?+(){}^$\\[\\\\]");
26 // $& is a perl-like expression that expands to all of the current match
27 // The '$' must be prefixed with the escape character '\' for
28 // boost to treat it as a literal.
29 // Thus, to prefix a matched expression with '\', we use:
30 return boost::regex_replace(expr, reg, "\\\\$&");
34 typedef std::map<string, string> InfoMap;
36 // A functor for use with std::find_if, used to ascertain whether a
37 // data entry matches the required regex_
38 // This class is unfortunately copied from ../frontend_helpers.cpp, so we should
39 // try to make sure to keep the two in sync.
40 class RegexMatch : public std::unary_function<string, bool>
43 // re and icase are used to construct an instance of boost::RegEx.
44 // if icase is true, then matching is insensitive to case
45 RegexMatch(InfoMap const & m, string const & re, bool icase)
46 : map_(m), regex_(re, icase) {}
48 bool operator()(string const & key) const {
49 // the data searched is the key + its associated BibTeX/biblio
52 InfoMap::const_iterator info = map_.find(key);
53 if (info != map_.end())
54 data += ' ' + info->second;
56 // Attempts to find a match for the current RE
58 return boost::regex_search(data, regex_);
62 mutable boost::regex regex_;
66 void test_escape_special_chars()
68 cout << escape_special_chars("abcd") << endl;
69 cout << escape_special_chars("ab&cd") << endl;
70 cout << escape_special_chars(".|*?+(){}[]^$\"") << endl;
71 cout << escape_special_chars("..||**??++(()){{}}[[]]^^$$\\\\") << endl;
75 void test_RegexMatch()
81 RegexMatch rm(im, "h.*o", false);
83 cout << rm("hello") << endl;
84 cout << rm("hei") << endl;
86 catch (boost::regex_error & regerr) {
87 cout << regerr.what() << endl;
94 test_escape_special_chars();