Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

[Solved]How to change the sub expression of QRegularExpression to lowercase or uppercase?



  • I want to change the following string from lower case to upper case

    string : 5am_006_01_05
    regex : (5am)(\d+)
    replace : \2
    \U\1
    expected output : 006_5AM_01_05
    true output : 006_\U5am_01_05

    @QString name("5am_006_01_05");
    name.replace(QRegularExpression{"(5am)(\d+)"}, "\2\U\1");@

    Do I miss anything?



  • I don't know if '\U' is supported in the output string.

    The replacement string is created using captured data '\1' '\2' ... in a way similar to how '%1' '%2' ... would be used with .arg(...). The second argument is not a regular expression but a string.

    Maybe some method like this might work (didn't try it)

    @
    name.replace(QRegular[removed]"reg-exp",QString("%1").arg(QString("\1").toUpper()));
    @



  • [quote author="Rondog" date="1422812141"]I don't know if '\U' is supported in the output string.

    The replacement string is created using captured data '\1' '\2' ... in a way similar to how '%1' '%2' ... would be used with .arg(...). The second argument is not a regular expression but a string.

    Maybe some method like this might work (didn't try it)
    [/quote]

    Thanks, but looks like it cannot work

    @QString name("5am_006_01_05");
    name.replace(QRegularExpression{"(5am)_(\d+)"},
    QString("%1").arg(QString("\1").toUpper()));
    qDebug()<<name;@

    actual result : "5am_01_05"


  • Lifetime Qt Champion

    Hi,

    @name = name.replace(QRegular[removed]"(\d+am)(\d+)"), QString("\2\1")).toUpper();@

    should do what you need



  • [quote author="SGaist" date="1423169881"]Hi,

    @name = name.replace(QRegular[removed]"(\d+am)(\d+)"), QString("\2\1")).toUpper();@

    should do what you need[/quote]

    Thanks, but this solution would change the whole words into uppercase

    Example :

    @QString name{"5am_006_01_05_hhi"};
    name = name.replace(QRegularExpression{"(\d+am)(\d+)"},
    QString("\2
    \1")).toUpper();
    qDebug()<<name;@

    The output will become "006_5AM_01_05_HHI"
    but the desired result is "006_5AM_01_05_hhi" if \Ucan works


  • Lifetime Qt Champion

    That's the kind of detail that's good to know.
    \U is not implemented

    @
    QString name("5am_006_01_05_hi_hi");
    QRegularExpression rx("(\d+am_)(\d+_)(.*)");
    QRegularExpressionMatch match = rx.match(name);
    name = match.captured(2) + match.captured(1).toUpper() + match.captured(3);
    @



  • [quote author="SGaist" date="1423349356"]That's the kind of detail that's good to know.
    \U is not implemented

    @
    QString name("5am_006_01_05_hi_hi");
    QRegularExpression rx("(\d+am_)(\d+_)(.*)");
    QRegularExpressionMatch match = rx.match(name);
    name = match.captured(2) + match.captured(1).toUpper() + match.captured(3);
    @[/quote]

    Thanks, this solution works. Since \U is not implemented yet, I could use it as a temporary solution.

    PS : check out boost regex, looks like they haven't implemented \U either


Log in to reply