我正在解析一个包含以下数据包的文件:
[propertyID="123000"] {
fillColor : #f3f1ed;
minSize : 5;
lineWidth : 3;
}为了只扫描这个[propertyID="123000"]片段,我有这个QRegExp
QRegExp("^\b\[propertyID=\"c+\"\]\b");但这不管用吗?下面是解析上述文件示例代码:
QRegExp propertyIDExp= QRegExp("\\[propertyID=\".*\"]");
propertyIDExp.setMinimal(true);
QFile inputFile(fileName);
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
while (!in.atEnd())
{
QString line = in.readLine();
// if does not catch if line is for instance
// [propertyID="123000"] {
if( line.contains(propertyIDExp) )
{
//.. further processing
}
}
inputFile.close();
}发布于 2015-08-19 19:46:54
使用以下表达式:
QRegExp("\\[propertyID=\"\\d+\"]");请参阅regex demo
在Qt正则表达式中,需要使用双反斜杠对正则表达式特殊字符进行转义,要匹配数字,可以使用速记类\d。此外,\b单词边界阻止了正则表达式匹配,因为它不能在字符串start和[之间以及]和空格之间进行匹配(或者改用\B )。
要匹配引号之间的任何内容,请使用一个被否定的字符类:
QRegExp("\\[propertyID=\"[^\"]*\"]");请参阅another demo
作为另一种选择,您可以在.*和QRegExp::setMinimal()的帮助下使用惰点匹配
QRegExp rx("\\[propertyID=\".*\"]");
rx.setMinimal(true);在Qt中,.可以匹配包括换行符在内的任何字符,因此请谨慎使用此选项。
发布于 2015-08-19 19:53:25
QRegExp("\\[propertyID=\".+?\"\\]")您可以使用..It将匹配除newline.Also之外的任何字符,使用+?使其不贪婪,否则它将在同一行的最后一个"实例处停止
https://stackoverflow.com/questions/32094589
复制相似问题