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. QString: replace white space behind "</tr>"

QString: replace white space behind "</tr>"

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 1.6k Views
  • 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by sonichy
    #1

    I want a HTML indent function in QTextEdit.
    Replace white space:

    s = s.replace(QRegExp("[\\s]+"), "") 
    

    I want something like:

    s = s.replace("</tr>white space", "</tr>\n") 
    

    https://github.com/sonichy

    KillerSmathK 1 Reply Last reply
    0
    • sonichyS sonichy

      I want a HTML indent function in QTextEdit.
      Replace white space:

      s = s.replace(QRegExp("[\\s]+"), "") 
      

      I want something like:

      s = s.replace("</tr>white space", "</tr>\n") 
      
      KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by
      #2

      @sonichy
      You must to scape the slash caracter \\/ in regular expression. Further you can use \\1 to insert the first captured group (</tr>) on replaced string.

      See an example:

      s.replace(QRegularExpression("(<\\/tr>)\\s"), "\\1\n");
      

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      sonichyS JonBJ 2 Replies Last reply
      1
      • KillerSmathK KillerSmath

        @sonichy
        You must to scape the slash caracter \\/ in regular expression. Further you can use \\1 to insert the first captured group (</tr>) on replaced string.

        See an example:

        s.replace(QRegularExpression("(<\\/tr>)\\s"), "\\1\n");
        
        sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on last edited by
        #3

        @KillerSmath Worked out, it is should be like these:

        s = s.replace(QRegExp("[\\s]+(<td>)"), "\\1");   // delete white space before <td>
        s = s.replace(QRegExp("[\\s]+(</td>)"), "\\1");   // delete white space before </td>
        s = s.replace(QRegExp("(</tr>)[\\s]+"), "\\1\n");   // delete white space after </tr>
        

        https://github.com/sonichy

        1 Reply Last reply
        1
        • KillerSmathK KillerSmath

          @sonichy
          You must to scape the slash caracter \\/ in regular expression. Further you can use \\1 to insert the first captured group (</tr>) on replaced string.

          See an example:

          s.replace(QRegularExpression("(<\\/tr>)\\s"), "\\1\n");
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @KillerSmath

          s.replace(QRegularExpression("(<\\/tr>)\\s"), "\\1\n");
          

          Your answer is useful to the rest of the world, but for anyone reading this you have erroneously inserted a \\, before /tr, which should be removed.

          KillerSmathK 1 Reply Last reply
          1
          • JonBJ JonB

            @KillerSmath

            s.replace(QRegularExpression("(<\\/tr>)\\s"), "\\1\n");
            

            Your answer is useful to the rest of the world, but for anyone reading this you have erroneously inserted a \\, before /tr, which should be removed.

            KillerSmathK Offline
            KillerSmathK Offline
            KillerSmath
            wrote on last edited by KillerSmath
            #5

            @JonB
            Sorry but i used \\ to escape a backward slash to string because / in means "start or end of a regular expression" and it needs to be escaped.

            Usage Example in regular expression
            Forward slash explanation

            @Computer Science Student - Brazil
            Web Developer and Researcher
            “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

            JonBJ 1 Reply Last reply
            0
            • KillerSmathK KillerSmath

              @JonB
              Sorry but i used \\ to escape a backward slash to string because / in means "start or end of a regular expression" and it needs to be escaped.

              Usage Example in regular expression
              Forward slash explanation

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @KillerSmath
              I've used regular expressions in various languages for hundreds of years, and I've never escaped a /. Nor has / ever meant "start of regular expression" as per your sources! So I'm looking into this mystery (for me) now...!

              I'm lost. This is all to do with "using / as the start of a regular expression". But isn't that JavaScript only? Every example on your references has /pattern/, that's not part of regular expressions. I think that's only because they are for using JS string.replace(/.../) "regular expression literal" syntax, no? Why would that apply to QRegularExpression?

              1 Reply Last reply
              1
              • fcarneyF Offline
                fcarneyF Offline
                fcarney
                wrote on last edited by
                #7

                Or you could use the new C++ 11 literals:

                R"((</tr>)[\s]+)"
                R"delimiter((</tr>)[\s]+)delimiter"
                

                No idea what they were smoking on the delimeter( version.

                C++ is a perfectly valid school of magic.

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

                  Hi,

                  @KillerSmath what tools are you using for regular expressions ?

                  If you take VI for example, / are used to separate elements when doing a search and replace for example. Same with the sed command. With sed you can change the separation char if you need to use / in an expression.

                  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