Important: Please read the Qt Code of Conduct -

Find QRegExp in the text

  • Hellow, i need find some RegExp construction in the QPlainTextEdit, i try do it
    @ QTextCursor curs;
    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!

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

  • Dont work:(

  • Might the cursor then be at the document end?

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

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

  • 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]

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

  • 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": to test regular expressions in Qt.

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

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

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

    [EDIT: code formatting! Volker]

  • 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.

  • 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.

  • 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:


    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?

Log in to reply