Important: Please read the Qt Code of Conduct -

Converting search string to QRegularExpression pattern

  • I have a widget that has API to set a QRegularExpression that will be used to highlight certain contents of the widget. The user is able to enter a regular expression in a QLineEdit and everything works quite well. I'd now like to give the user the ability to toggle between "regex mode" and "plain string compare" mode as some users might not know regex and others are annoyed to escape all the characters if they are looking for something with special symbols.

    My question: Is there any way I can convert the "plain string" to a valid QRegularExpression? I'd like to avoid modifying said widget to offer search & highlight capabilities by just doing good old QString::compare(). I looked through the docs but it seems that QRegularExpression doesn't offer such a feature and it's not enough that I just escape any non-alphanumeric character.

    I'd be thankful for any kind of help & advice.

  • Never mind. QRegularExpression::escape() does exactly what I want.

Log in to reply