Qt-检查正则表达式宽度

时间:2015-11-05 08:16:37

标签: c++ regex qt

我正在尝试用不同的表达式替换正则表达式,这取决于行的宽度和框的宽度。

这是我的代码:

//mangledText is my text that I've searching on it
//rx is regular expression
QRegExp rx("<lms([^<]*)/>");
    while ((pos = rx.indexIn(mangledText)) != -1){
     for (int j = 0; j < tempLayout->lineCount(); j++){         
        QTextLine tl = tempLayout->lineAt(j);
            //here is width of each line        
        int naturalTextWidth = tl.naturalTextWidth();
            //rect width is maximum width of box
        if (naturalTextWidth < rectWidth)
            mangledText.replace(pos, rx.matchedLength(), "replace Text");
        else
            mangledText.replace(pos, rx.matchedLength(), "\n replace Text");             
    }
}
mangledText.replace('\n', QChar::LineSeparator);

如果该行上的文字不在框中,我想用“\ n替换文本”替换正则表达式。否则我用“替换文字”替换它。问题是它总会将它转移到下一行。因为rectWidth小于naturalTextWidth。但我想检查每个正则表达式来替换。

更新: 例如 : 111111111111111111111111111 <lms8><lms3><lms2>

显示:

111111111111111111111111111
<lms8>
<lms3>
<lms2>

我想要这个:

111111111111111111111111111
<lms8><lms3><lms2>

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

试试这个。

(.[^<]*)(<lms.*>)

替换为以下语法。

$1\n$2

结果将是

111111111111111111111111111 <lms8><lms3><lms2>

相关问题