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] I could not change in a special part of string by using Qregexp
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] I could not change in a special part of string by using Qregexp

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 1.9k Views 3 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.
  • kybernetesK Offline
    kybernetesK Offline
    kybernetes
    wrote on last edited by kybernetes
    #1

    Hello,

    i am trying to change a specific part of string. my string like this.
    "size 2 - (48-50 size)"

    and i want to change it like
    "size 2 - (48&50 size)"

    because im using - char for split size and color in my string list.
    i cant use change the last - to & because this is a special condition.

    i use that code:
    QString fullVariants = "size 2 - (48-50 size)"
    fullVariants.replace(QRegExp("[0-9]{1,2}\-[0-9]{1,2}"), "\1&\2");
    qDebug() << fullVariants;

    but debug says, "size 2 (& size)"

    can you help me? i didnt undertand why /1 and /2 doesnt work for changing a part of string.
    Thanks.

    1 Reply Last reply
    0
    • W Offline
      W Offline
      wlof
      wrote on last edited by
      #2

      You need to escape the backslashes in the after argument:

      fullVariants.replace(QRegExp("[0-9]{1,2}\-[0-9]{1,2}"), "\\1&\\2");
      
      kybernetesK 1 Reply Last reply
      0
      • W wlof

        You need to escape the backslashes in the after argument:

        fullVariants.replace(QRegExp("[0-9]{1,2}\-[0-9]{1,2}"), "\\1&\\2");
        
        kybernetesK Offline
        kybernetesK Offline
        kybernetes
        wrote on last edited by
        #3

        @wlof it gives like that 'size 2 (\1&\2 size)' it does not work..

        1 Reply Last reply
        0
        • W Offline
          W Offline
          wlof
          wrote on last edited by wlof
          #4

          Ah, that's because you're missing the capturing parentheses in the regexp. Try this:

          fullVariants.replace(QRegExp("([0-9]{1,2})\-([0-9]{1,2})"), "\\1&\\2");
          
          kybernetesK 1 Reply Last reply
          1
          • M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            Hi and welcome to devnet,

            you have also to capture the components

            fullVariants.replace(QRegExp("([0-9]{1,2})-([0-9]{1,2})"), "\\1&\\2");
            

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            1 Reply Last reply
            1
            • W wlof

              Ah, that's because you're missing the capturing parentheses in the regexp. Try this:

              fullVariants.replace(QRegExp("([0-9]{1,2})\-([0-9]{1,2})"), "\\1&\\2");
              
              kybernetesK Offline
              kybernetesK Offline
              kybernetes
              wrote on last edited by
              #6

              @wlof it works, thank you very much.

              1 Reply Last reply
              0
              • W Offline
                W Offline
                wlof
                wrote on last edited by
                #7

                You're welcome! :)

                Remember to mark the topic as solved!

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mcosta
                  wrote on last edited by
                  #8

                  @kybernetes Keep in mind that \- is not recognized

                  Once your problem is solved don't forget to:

                  • Mark the thread as SOLVED using the Topic Tool menu
                  • Vote up the answer(s) that helped you to solve the issue

                  You can embed images using (http://imgur.com/) or (http://postimage.org/)

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

                    Hi and welcome to devnet,

                    To help you build regular expression with QRegExp there's the RegExp Tool from Qt's example.

                    If you're using Qt 5 you should also consider using QRegularExpression which is more powerful and should be faster.

                    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

                    • Login

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