Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]How to change the sub expression of QRegularExpression to lowercase or uppercase?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stereomatching
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rondog
      wrote on last edited by
      #2

      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()));
      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stereomatching
        wrote on last edited by
        #3

        [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"

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

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

          should do what you need

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            stereomatching
            wrote on last edited by
            #5

            [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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S Offline
                S Offline
                stereomatching
                wrote on last edited by
                #7

                [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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved