Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Find QRegExp in the text

    General and Desktop
    5
    13
    5329
    Loading More Posts
    • 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.
    • R
      Ruzik last edited by

      Hellow, i need find some RegExp construction in the QPlainTextEdit, i try do it
      @ QTextCursor curs;
      curs.setPosition(-1);
      if (plainTextEdit->document()->find(QRegExp("{. }"))!=curs)
      listWidget->addItem("empty stylesheet {it's empty!}");@
      I want if there are { } in the text that program add item in the listWidget.
      I think that i wrong chek position in the if(), can you say me how can chek this position right way
      Advance many thanks for your help!

      1 Reply Last reply Reply Quote 0
      • F
        Franzk last edited by

        try
        @if (!plainTextEdit->document()->find(QRegExp("{. }")).isNull()))
        listWidget->addItem(...);@

        "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply Reply Quote 0
        • R
          Ruzik last edited by

          Dont work:(

          1 Reply Last reply Reply Quote 0
          • F
            Franzk last edited by

            Might the cursor then be at the document end?

            "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply Reply Quote 0
            • G
              goetz last edited by

              Curly brackets have a special meaning in regular expressions. You need to escape them like this:

              @
              QRegExp re("\{. \}");
              @

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply Reply Quote 0
              • R
                Ruzik last edited by

                I find my mistake
                @ if (!plainTextEdit->document()->find(QRegExp("\{ *\}")).isNull())
                listWidget->addItem("empty stylesheet {it's empty!}");@
                And code work, but it is not work with sign of new string(\n), how can i modif my QRegExp to my code work, i try

                @
                QRegExp("\{[ \Z]\}"))
                QRegExp("\{[ \Z]
                \}"))
                @

                And instead Z i try use A
                All does not work

                [EDIT: wrap your code!, Volker]

                1 Reply Last reply Reply Quote 0
                • R
                  Ruzik last edited by

                  I also try QRegExp(”\{[ \n]*\}”))

                  1 Reply Last reply Reply Quote 0
                  • S
                    soroush last edited by

                    AFAIK you can apply a regular expression in a single text block only. That is you cannot use \n notation with your regexp. I remember that in my homework I had to break my expression to two expressions to do that. I didn't found a way to use a regexp with two paragraphs in QPlainTextEdit....

                    you can use "this tool":http://sourceforge.net/projects/regexer to test regular expressions in Qt.

                    1 Reply Last reply Reply Quote 0
                    • R
                      Ruzik last edited by

                      But there are operators \A and \Z - start and end of string in the QRegExp, so in QRegExp must work with it

                      1 Reply Last reply Reply Quote 0
                      • G
                        goetz last edited by

                        [quote author="Ruzik" date="1307644633"]I also try

                        @
                        QRegExp(”\{[ \n]*\}”))[/quote]
                        @

                        [EDIT: code formatting! Volker]

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply Reply Quote 0
                        • S
                          soroush last edited by

                          QRegExp works with \n and so on but in QPlainTextEdit it's applied on a text block. not whole contents of QPlainTextEdit. you probably need to break your expression or use that on the QPlainTextEdit::toPlainText() to find begin and end indexes.

                          1 Reply Last reply Reply Quote 0
                          • G
                            goetz last edited by

                            The search pattern is only applied for a text block. The pattern can not cross a text block boundary! See method static bool findInBlock() around line 1290 in qtextdocument.cpp of the Qt sources.

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            1 Reply Last reply Reply Quote 0
                            • D
                              dcortesi last edited by

                              The answers above explain a problem I came to ask about, so thank you. However, in the general case of a user-entered pattern I do not see any way of getting around this bad restriction.

                              Suppose the pattern (example from QRegExp documentation) is <b>.*</b>, this cannot succeed when the document is:

                              @
                              some text or <b>bold
                              text</b> other etc.
                              @

                              You could examine the user's pattern and change it into:

                              @
                              <b>.$
                              ^.
                              </b>
                              @

                              But that would fail for the case of <b>on one line</b> and also would fail for the case of a target spanning three or more lines. In short I see no work-around for the general case of patterns matching across multiple lines. Comment?

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post