regexqtregex-groupqregexpqregularexpression

How to combine RegEx conditions?


I need to make a QLineEdit with a QRegularExpressionValidator with the following 3 constraints:

  1. Cannot start with empty space ^[\\S]
  2. Cannot start with Hello ^(?!Hello).+
  3. Cannot end with empty space ^.*[\\S]$

How do I combine those 3 in one regex so that I can set it a QRegularExpressionValidator?

Thank you!

Note: As long as I have a regex that is verifiable with a regex tool I am good. I have specified Qt just to provide more context.


Solution

  • You could use

    ^(?!Hello\b)\S(?:.*\S)?$
    

    Explanation

    Regex demo