Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 如何替换QString中的数字文本组合_Qt - Fatal编程技术网

Qt 如何替换QString中的数字文本组合

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}表示匹配至少包含一个数字但不超过十个的数

这不会将我的字符串更改为“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}表示匹配至少包含一个数字但不超过十个的数字序列

因此,即使pOutputIndexFile为“G:\new964.dat”且文件号为“965.dat”,结果也是:


“G:\new965.dat”

您可以尝试以下方法:

pOutputIndexFile->replace(QRegExp("\\d{1,10}.dat"),fileNumber);
  • [0-9]
    :表示一个数字
  • {1}
    :表示正好一个字符
  • \.
    :保护
  • dat
    :dat
我没问题

pOutputIndexFile->replace(QRegExp("[0-9]{1}\.dat"),fileNumber);

好吧,看看你的常规表达中的扩展部分那是打字错误。。我在问题中编辑删除了第一个“\\”[”被视为文字。
 pOutputIndexFile->replace(QRegExp("\\d\\d?.dat"),fileNumber);