0

標準化されていない HTML (多数の Wiki マークアップと混在) が多数あり、そこから特定のタグとさまざまな括弧を削除する必要があります。

ここで単純な文字列が示すように、QRegularExpression はジョブに適したツールではありません。

myString =
QString("yes<tag par1='x'>no<tag par2='y'>no</tag>no</tag>yes<tag>no</tag>yes")

// Won't work | This matches the first nested </tag>
myString.replace(QRegularExpression("<tag param1='x'>(.+?)</tag>"),"\\1")

// Won't Work | This matches the last </tag>, removing the second "yes"
myString.replace(QRegularExpression("<tag param1='x'>(.+)</tag>"),"\\1")

理想的には、5 つのパラメーターを指定する関数が最適であると考えています。

QString stripCustomBrackets(
        QString input,                     // myString
        QRegularExpression openingBracket, // Eg. "<tag>"  or "{{["
        QRegularExpression openingIdentify,// Eg. "<tag par1='x'>"
                        // par1='x' identifies the tag to work with.

        QRegularExpression closingBracket, // Eg. "</tag>" or "]}}"
        QRegularExpression closingIdentify,// Eg. "FooBar</tag>"
                        // Means you only capture tags with FooBar at the end.

        // <tag> keep text if true </tag>
        bool capture = false) {

    QString output;
    if ( /* Number of openingBrackets equally match closingBrackets */ ) {
        if (capture) { 
            /* Do code where you leave the contents in between the brackets */ 
        } else {
            /* Do code where you remove the contents in between the brackets */
        }
        return output;

    } else {
        qDebug() << "Unable to consolidate;" << endl
                 << openingBracket << " count = " << /* count */ << endl
                 << closingBracket << " count = " << /* count */ << endl
                 << "Brackets do not match each other in number.";
        return input;
    }
}
qDebug() << stripCustomBrackets(mystring, 
    QRegularExpression("<tag"),
    QRegularExpression(" par1='x'>"),
    QRegularExpression("</tag>"),
    QRegularExpression(""),
    true);

qDebug() << stripCustomBrackets(mystring, 
    QRegularExpression("<tag"),
    QRegularExpression(" par2='y'>"),
    QRegularExpression("</tag>"),
    QRegularExpression(""),
    false);

qDebug() << stripCustomBrackets(mystring, 
    QRegularExpression("<tag"),
    QRegularExpression(" par[0-9]='[a-z]'>"),
    QRegularExpression("</tag>"),
    QRegularExpression(""),
    false); 

qDebug() << stripCustomBrackets(mystring, 
    QRegularExpression("<tag "),
    QRegularExpression(""),
    QRegularExpression("No</tag>"),
    QRegularExpression(""),
    false); 

_

"yesno<tag par2='y'>no</tag>noyes<tag>no</tag>yes"

"yes<tag par1='x'>nono</tag>yes<tag>no</tag>yes"

"yesyes<tag>no</tag>yes"

"Unable to consolidate;"
"'<tag '    Count = 2"
"'No</tag>' Count = 3"
"Brackets do not match each other in number.";
"yes<tag par1='x'>no<tag par2='y'>no</tag>no</tag>yes<tag>no</tag>yes"

これを達成するための最も信頼性が高く安定した方法は何ですか?

4

1 に答える 1