Qt-检查正则表达式宽度

Qt- checking regular expression width

本文关键字:正则表达式 检查 Qt-      更新时间:2023-10-16

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

这是我的代码:

//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);

如果该行的文本超出了框外,我希望将正则表达式替换为"替换文本"。否则我将其替换为"替换文本"。问题是它总是会把它移到下一行。因为CCD_ 1小于CCD_。但我想检查要替换的每个正则表达式。

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

正在显示:

1111111111111111111
<lms8>
<lms3>
<lms2>

我想要这个:

1111111111111111111
<lms8><lms3><lms2>

有什么建议吗?

试试这个。

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

替换为以下语法。

$1n$2

结果将是

111111111111111111111111111 <lms8><lms3><lms2>