qmake language 的字符串替换操作的规则为:
VAR ~= s[seprator]pattern[seprator]replace[seprator]?[gqi]
1、必须以s开头
2、seprator需要自己指定,可以是任意字符
3、pattern为正则表达式的内容,可以参看正则表达式规则:正则表达式(全)
4、replace表示要替换成的目标内容,用于替换VAR中存放的字符串中满足pattern的部分
5、seprator最多有三个,pattern和replace中不能含有seprator,含有三个以上的seprator会被报错。
6、gqi是可选的,可单选或多选,顺序随意,可有可无。
(1)VAR是一个字符串数组类型,是要操作的对象。g表示对VAR中所有字符串单元都做操作,没有g则只对VAR中第一个字符串单元做操作。VAR字符串单元解析时以双引号为单元,无双引号时以空格或换行符为间隔,比如"aa bb"表示一个字符串单元。aa bb(等价于"aa" "bb"),表示VAR有两个字符串单元。
(2)q表示对pattern中的特殊字符进行转义
(3)i表示正则表达式匹配规则区分大小写。
- #用 '/' 做seprator
- VAR = helLo world,for fun(tt)
- VAR ~= s/l/t/gi #将VAR中所有字符串单元的l替换成t,不区分大小写
- message($$VAR) #输出:Project MESSAGE: hetto wortd,for fun(tt)
-
- VAR = helLo world,for fun(tt)
- VAR ~= s/(tt)/-/gi #将括号中的字符串"tt"替换成"-"
- message($$VAR) #输出:Project MESSAGE: hetto wortd,for fun(-)
-
- VAR = helLo world,for fun(tt)(TT)
- VAR ~= s/(tt)/-/qg #先对pattern进行转义,pattern变成"\(tt\)",然后将VAR中存放的所有字符串单元单元中的"(tt)"用"-"进行替换,区分大小写。等价于VAR~=s/\(tt\)/-/g
- message($$VAR) #输出:Project MESSAGE: hetto wortd,for fun-(TT)
-
- #用 '\'' 做seprator
- VAR = helLo world,for fun(tt)
- VAR ~= s\'[()]\'-\'g #对VAR中存放的所有字符串单元中的'('和')'用"-"进行替换
- message($$VAR) #输出:Project MESSAGE: hetto wortd,for fun-tt-
杂谈:qmake解析正则表达式的源码
- //E:\workspace\QtWork\qmake\library\qmakeevaluator.cpp
- QMakeEvaluator::VisitReturn QMakeEvaluator::visitProVariable(
- ushort tok, const ProStringList &curr, const ushort *&tokPtr)
- {
- .......
- if (tok == TokReplace) { // ~=
- // DEFINES ~= s/a/b/?[gqi]
-
- ProStringList varVal;
- if (expandVariableReferences(tokPtr, sizeHint, &varVal, true) == ReturnError)
- return ReturnError;
- const QStringRef &val = varVal.at(0).toQStringRef();
- if (val.length() < 4 || val.at(0) != QLatin1Char('s')) {
- evalError(fL1S("The ~= operator can handle only the s/// function."));
- return ReturnTrue;
- }
- QChar sep = val.at(1);
- auto func = val.split(sep, QString::KeepEmptyParts);
- if (func.count() < 3 || func.count() > 4) {
- evalError(fL1S("The s/// function expects 3 or 4 arguments."));
- return ReturnTrue;
- }
-
- bool global = false, quote = false, case_sense = false;
- if (func.count() == 4) {
- global = func[3].indexOf(QLatin1Char('g')) != -1;
- case_sense = func[3].indexOf(QLatin1Char('i')) == -1;
- quote = func[3].indexOf(QLatin1Char('q')) != -1;
- }
- QString pattern = func[1].toString();
- QString replace = func[2].toString();
- if (quote)
- pattern = QRegExp::escape(pattern);
-
- QRegExp regexp(pattern, case_sense ? Qt::CaseSensitive : Qt::CaseInsensitive);
-
- // We could make a union of modified and unmodified values,
- // but this will break just as much as it fixes, so leave it as is.
- replaceInList(&valuesRef(varName), regexp, replace, global, m_tmp2);
- debugMsg(2, "replaced %s with %s", dbgQStr(pattern), dbgQStr(replace));
- }
- ...
- }
-
- -----------------
- //D:\Qt\Qt5.12.0\5.12.0\Src\qtbase\src\corelib\tools\qregexp.cpp
- QString QRegExp::escape(const QString &str)
- {
- QString quoted;
- const int count = str.count();
- quoted.reserve(count * 2);
- const QLatin1Char backslash('\\');
- for (int i = 0; i < count; i++) {
- switch (str.at(i).toLatin1()) {
- case '$':
- case '(':
- case ')':
- case '*':
- case '+':
- case '.':
- case '?':
- case '[':
- case '\\':
- case ']':
- case '^':
- case '{':
- case '|':
- case '}':
- quoted.append(backslash);
- }
- quoted.append(str.at(i));
- }
- return quoted;
- }