我可以幫你用下面的代碼:
QString string1 = "a b c d e f g h i j k l m n o p q r s t u v w y z";
QRegExp regExp("(a|e|i|o|u|y)");//letters to be escaped with a backslash, brackets are used to capture the regular expressions in the current string
string1.replace(regExp,"(\\1)");//Puts each letter a, e, i, o, u, y in brackets
//string1=="(a) b c d (e) f g h (i) j k l m n (o) p q r s t (u) v w (y) z"
string1 = "a b c d e f g h i j k l m n o p q r s t u v w y z";
QRegExp regExp2("a|e|i|o|u|y");
string1.replace(regExp2,"(\\1)");//Replace each letter a, e, i, o, u, y by the string (\1)
//string1=="(\1) b c d (\1) f g h (\1) j k l m n (\1) p q r s t (\1) v w (\1) z"
在您的情況:
QString reserved = "command1,command2,command3";
QString copyOfReserved = reserved;
copyOfReserved.replace(",","|");
//copyOfReserved == "command1|command2|command3"
copyOfReserved = "\\b("+copyOfReserved+")\\b");
//copyOfReserved == "\b(command1|command2|command3)\b"
QString a = "\\command1 \\command2command1 \\command3 command2 sometext";
QString b = a;
b.replace(QRegExp("\\\\(" + copyOfReserved + ")"),"<input com=\"\\1\"></input>");
//b == "<input com="command1"></input> \command2command1 <input com="command3"></input> command2 sometext"
我希望我幫你。
[QString :: contains](http://qt-project.org/doc/qt-5.0/qtcore/qstring.html#contains-5)和[QRegExp](http://qt-project.org /doc/qt-5.0/qtcore/qregexp.html) – Huy