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. `find()` to search for the newline character via QRegExp
Forum Updated to NodeBB v4.3 + New Features

`find()` to search for the newline character via QRegExp

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 2.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.
  • T Offline
    T Offline
    tamlok
    wrote on last edited by tamlok
    #1

    Is there any way to search the newline character \n in QTextEdit using its find(QRegExp) method?

    BTW, searching this will hang the find() function:

    QTextEdit *edit = new QTextEdit();
    edit->find(QRegExp("$"));
    

    Thanks!

    Gojir4G 1 Reply Last reply
    0
    • T tamlok

      Is there any way to search the newline character \n in QTextEdit using its find(QRegExp) method?

      BTW, searching this will hang the find() function:

      QTextEdit *edit = new QTextEdit();
      edit->find(QRegExp("$"));
      

      Thanks!

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      @tamlok Hi,

      Usually QTextEdit use the unicode character "Paragraph Separator", which is \u2029.

      I have used this regex to find all new line in text edit content:

      QRegularExpression("\u2029|\r\n|\r|\n")
      

      Works fine for me.

      JonBJ T 2 Replies Last reply
      4
      • Gojir4G Gojir4

        @tamlok Hi,

        Usually QTextEdit use the unicode character "Paragraph Separator", which is \u2029.

        I have used this regex to find all new line in text edit content:

        QRegularExpression("\u2029|\r\n|\r|\n")
        

        Works fine for me.

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

        @Gojir4
        Is that really what you use in your C++ source code?? Those \r & \ns are going to be passed as literal carriage-returns/newlines to construct the regular expression, which to me is not a good idea at all.

        Better surely (standard C++)

            QRegularExpression("\u2029|\\r\\n|\\r|\\n")
        
        Gojir4G 1 Reply Last reply
        5
        • JonBJ JonB

          @Gojir4
          Is that really what you use in your C++ source code?? Those \r & \ns are going to be passed as literal carriage-returns/newlines to construct the regular expression, which to me is not a good idea at all.

          Better surely (standard C++)

              QRegularExpression("\u2029|\\r\\n|\\r|\\n")
          
          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          @JonB yep, you're right. Noob mistake. I dont know if this is an issue when use with QTextDocument based widget. But I will correct it in my code anyway. Thanks for pointing me this.

          1 Reply Last reply
          1
          • Gojir4G Gojir4

            @tamlok Hi,

            Usually QTextEdit use the unicode character "Paragraph Separator", which is \u2029.

            I have used this regex to find all new line in text edit content:

            QRegularExpression("\u2029|\r\n|\r|\n")
            

            Works fine for me.

            T Offline
            T Offline
            tamlok
            wrote on last edited by
            #5

            @Gojir4 Thanks! Are you using the QTextEdit::find() interface? find() seems only have QRegExp overloaded prototype and QRegExp("\x2029") does not work. BTW, do you know why find(QRegExp("$")) will hang the application?

            Thanks again!

            JonBJ Gojir4G 2 Replies Last reply
            0
            • T tamlok

              @Gojir4 Thanks! Are you using the QTextEdit::find() interface? find() seems only have QRegExp overloaded prototype and QRegExp("\x2029") does not work. BTW, do you know why find(QRegExp("$")) will hang the application?

              Thanks again!

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

              @tamlok said in `find()` to search for the newline character via QRegExp:

              QRegExp("\x2029")

              But the example for you to copy was "\u2029". \x... will not work.

              T 1 Reply Last reply
              1
              • JonBJ JonB

                @tamlok said in `find()` to search for the newline character via QRegExp:

                QRegExp("\x2029")

                But the example for you to copy was "\u2029". \x... will not work.

                T Offline
                T Offline
                tamlok
                wrote on last edited by
                #7

                @JonB From the doc, it says:

                \xhhhh	Matches the Unicode character corresponding to the hexadecimal number hhhh (between 0x0000 and 0xFFFF).
                

                \u2029 does not work, either.

                JonBJ 1 Reply Last reply
                0
                • T tamlok

                  @JonB From the doc, it says:

                  \xhhhh	Matches the Unicode character corresponding to the hexadecimal number hhhh (between 0x0000 and 0xFFFF).
                  

                  \u2029 does not work, either.

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

                  @tamlok
                  Sorry, OK to the \x rather than the \u, I'm not a C++-er.

                  In that case, I presume your "does not work" means that you have checked the return result of your QTextEdit::find(QRegExp("\x2029")) and that's false, right? What makes you think your text even has a \x2029 anywhere in it? If it's not finding it, I presume it does not.

                  T 1 Reply Last reply
                  0
                  • T tamlok

                    @Gojir4 Thanks! Are you using the QTextEdit::find() interface? find() seems only have QRegExp overloaded prototype and QRegExp("\x2029") does not work. BTW, do you know why find(QRegExp("$")) will hang the application?

                    Thanks again!

                    Gojir4G Offline
                    Gojir4G Offline
                    Gojir4
                    wrote on last edited by
                    #9

                    @tamlok said in `find()` to search for the newline character via QRegExp:

                    find() seems only have QRegExp overloaded prototype

                    You can specify a PatternSyntax like QRegExp::FixedString, QRegExp::Wildcard, if you don't want to use regular expression.

                    @tamlok said in `find()` to search for the newline character via QRegExp:

                    BTW, do you know why find(QRegExp("$")) will hang the application?

                    Not really. That's seems strange. But probably this pattern is not well supported by QRegExp. There are some limitations with this class. That's the reason QRegularExpression was added in Qt to replace QRegExp. Unfortunately only QRegExp is currently supported with QTextEdit::find().

                    T 1 Reply Last reply
                    1
                    • Gojir4G Gojir4

                      @tamlok said in `find()` to search for the newline character via QRegExp:

                      find() seems only have QRegExp overloaded prototype

                      You can specify a PatternSyntax like QRegExp::FixedString, QRegExp::Wildcard, if you don't want to use regular expression.

                      @tamlok said in `find()` to search for the newline character via QRegExp:

                      BTW, do you know why find(QRegExp("$")) will hang the application?

                      Not really. That's seems strange. But probably this pattern is not well supported by QRegExp. There are some limitations with this class. That's the reason QRegularExpression was added in Qt to replace QRegExp. Unfortunately only QRegExp is currently supported with QTextEdit::find().

                      T Offline
                      T Offline
                      tamlok
                      wrote on last edited by
                      #10

                      @Gojir4 said in `find()` to search for the newline character via QRegExp:

                      @tamlok said in `find()` to search for the newline character via QRegExp:

                      find() seems only have QRegExp overloaded prototype

                      You can specify a PatternSyntax like QRegExp::FixedString, QRegExp::Wildcard, if you don't want to use regular expression.

                      No, I do want to use QRegExp to search for the "Paragraph Separator". But it seems not work (QRegExp("\x2029")).

                      @tamlok said in `find()` to search for the newline character via QRegExp:

                      BTW, do you know why find(QRegExp("$")) will hang the application?

                      Not really. That's seems strange. But probably this pattern is not well supported by QRegExp. There are some limitations with this class. That's the reason QRegularExpression was added in Qt to replace QRegExp. Unfortunately only QRegExp is currently supported with QTextEdit::find().

                      Emm... Seems that I have nothing to do but handle the corner cases manually.

                      1 Reply Last reply
                      0
                      • JonBJ JonB

                        @tamlok
                        Sorry, OK to the \x rather than the \u, I'm not a C++-er.

                        In that case, I presume your "does not work" means that you have checked the return result of your QTextEdit::find(QRegExp("\x2029")) and that's false, right? What makes you think your text even has a \x2029 anywhere in it? If it's not finding it, I presume it does not.

                        T Offline
                        T Offline
                        tamlok
                        wrote on last edited by tamlok
                        #11

                        @JonB said in `find()` to search for the newline character via QRegExp:

                        @tamlok
                        Sorry, OK to the \x rather than the \u, I'm not a C++-er.

                        In that case, I presume your "does not work" means that you have checked the return result of your QTextEdit::find(QRegExp("\x2029")) and that's false, right? What makes you think your text even has a \x2029 anywhere in it? If it's not finding it, I presume it does not.

                        I am sure the text will have \x2029 because I have multiple lines and paragraphs in QTextEdit, which surely has the "Paragraph Separator".

                        Anyway, it seems that QRegExp does not supoprt that well.

                        JonBJ 1 Reply Last reply
                        0
                        • T tamlok

                          @JonB said in `find()` to search for the newline character via QRegExp:

                          @tamlok
                          Sorry, OK to the \x rather than the \u, I'm not a C++-er.

                          In that case, I presume your "does not work" means that you have checked the return result of your QTextEdit::find(QRegExp("\x2029")) and that's false, right? What makes you think your text even has a \x2029 anywhere in it? If it's not finding it, I presume it does not.

                          I am sure the text will have \x2029 because I have multiple lines and paragraphs in QTextEdit, which surely has the "Paragraph Separator".

                          Anyway, it seems that QRegExp does not supoprt that well.

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

                          @tamlok
                          That \x pattern then is actually recognised by QRegExp. You do not just want to pass the C character, I think. So try: QTextEdit::find(QRegExp("\\x2029")) ?

                          Also read up the hidesousness of https://stackoverflow.com/questions/22838656/what-qstring-can-not-pass-a-qregexp-of-the-form-x00-xff

                          1 Reply Last reply
                          1
                          • VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by
                            #13

                            I think you can use QTextCursor::move(QTextCursor::EndOfLine) as an alternative.

                            Are you using the QTextEdit::find() interface? find() seems only have QRegExp overloaded prototype

                            This will be solved in Qt 5.13 thanks to @SGaist: https://codereview.qt-project.org/224913/

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            1 Reply Last reply
                            2

                            • Login

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