Qt 如何替换QString中的数字文本组合
这不会将我的字符串更改为“G:\new2.dat”。如何执行此操作?您的正则表达式正在查找扩展名为Qt 如何替换QString中的数字文本组合,qt,Qt,这不会将我的字符串更改为“G:\new2.dat”。如何执行此操作?您的正则表达式正在查找扩展名为idx的文件,而不是dat。由于找不到,因此不会替换任何内容。可以通过以下方式完成: pOutputIndexFile is "G:\new1.dat" fileNumber is "2.dat" pOutputIndexFile->replace(QRegExp("\\[0-9]{1,1}\\.dat"),fileNumber); \d{1,10}表示匹配至少包含一个数字但不超过十个的数
idx
的文件,而不是dat
。由于找不到,因此不会替换任何内容。可以通过以下方式完成:
pOutputIndexFile is "G:\new1.dat"
fileNumber is "2.dat"
pOutputIndexFile->replace(QRegExp("\\[0-9]{1,1}\\.dat"),fileNumber);
\d{1,10}表示匹配至少包含一个数字但不超过十个的数字序列
因此,即使pOutputIndexFile为“G:\new964.dat”且文件号为“965.dat”,结果也是:
“G:\new965.dat”您可以尝试以下方法:
pOutputIndexFile->replace(QRegExp("\\d{1,10}.dat"),fileNumber);
:表示一个数字[0-9]
:表示正好一个字符{1}
:保护\.
:datdat
pOutputIndexFile->replace(QRegExp("[0-9]{1}\.dat"),fileNumber);
好吧,看看你的常规表达中的扩展部分那是打字错误。。我在问题中编辑删除了第一个“\\”[”被视为文字。
pOutputIndexFile->replace(QRegExp("\\d\\d?.dat"),fileNumber);