http://server.com/$user/docs/$version
D:\share\$user\$$version
replace( "$$", "@MyCodeEscapeSymbol@");replace в циклеreplace("@MyCodeEscapeSymbol@", "$$");
C++ (Qt)#include <iostream>#include <string>#include <map>#include <boost/xpressive/xpressive.hpp> using namespace std;using namespace boost::xpressive; class substitute{public: substitute() { m_map["$user"] = "terminator"; m_map["$version"] = "T1000"; } string operator()(const smatch & match) const { auto it = m_map.find(match[1]); if (it != m_map.end()) return it->second; return match[1]; } private: map<string, string> m_map;}; int main(){ std::string in("http://server.com/$user/docs/$version D:\\share\\$user\\$$version"); sregex re = (s1= +as_xpr('$') >> +_w); std::string out = regex_replace(in, re, substitute()); std::cout << out << std::endl; return 0;}
Bashhttp://server.com/terminator/docs/T1000 D:\share\terminator\$$version
C++ (Qt)sregex re = (s1= +as_xpr('$') >> +_w);
C++ (Qt)#include <QCoreApplication>#include <QDebug>#include <QMap> #include "qregexreplace.h" class Substitute{public: Substitute() { m_map["$user"] = "terminator"; m_map["$version"] = "T1000"; } QString operator()(const QString & match) const { auto it = m_map.find(match); if (it != m_map.end()) return it.value(); return match; } private: QMap<QString, QString> m_map;}; int main(){ QString str = "http://server.com/$user/docs/$version D:\\share\\$user\\$$version"; QRegularExpression rx("(?<!\\$)(\\$\\w+)"); qDebug() << qRegexReplace(str, rx, Substitute()); return 0;}