0

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.

santahopar
  • 2,933
  • 2
  • 29
  • 50

2 Answers2

1

This should do it:

var strings = [
  'a',
  'this is ok',
  ' leading space',
  'trailing space ',
  'Hello text',
  'Hello'
];
var re = /^([^\s]|(?!(Hello|\s)).*[^\s])$/;
strings.forEach((str) => {
  var val = re.test(str);
  console.log('"' + str + '" ==> ' + val);
});

Console output:

"a" ==> true
"this is ok" ==> true
" leading space" ==> false
"trailing space " ==> false
"Hello text" ==> false
"Hello" ==> false

Explanation of the regex:

  • ^...$ -- anchor at the beginning and end
  • ([^\s]|...) -- logical or, where the first part is a single non space char
  • (?!(...)).+ -- negative lookahead
  • (Hello|\s) -- ... of 'Hello' or space
  • .+[^\s] -- followed by any number of chars, except space at the end
Peter Thoeny
  • 7,379
  • 1
  • 10
  • 20
  • It's working except that it expects at least two characters at the start of the string. Could you please fix that? So for instance if I just type "a" it should match, but I get no match. – santahopar Jul 25 '20 at 02:06
  • ```^(?!(RBX|\\s)).+$``` this seems to solve it, but then the last requirement of not ending with an empty space does not meet. – santahopar Jul 25 '20 at 02:17
  • Updated the regex with example to allow a single non-space char. Please accept if this solves your question. – Peter Thoeny Jul 25 '20 at 04:37
  • Just typed "Hello" at https://regex101.com and it accepted, which it shouldn't. The single non-space character at the beginning is OK. – santahopar Jul 25 '20 at 05:52
  • Did you try to tweak the regex? Please, at last show us that you make an effort, not just ask. – Peter Thoeny Jul 25 '20 at 23:37
  • In any case, I updated the answer with a `'Hello'` test case. – Peter Thoeny Jul 25 '20 at 23:41
  • `Hello` should not be legal, with your regex it shows as if its legal. And I did try to tweak it, but with no success. Please look at the answer above. That one works. I have however voted up on your answer since it also gives valuable information. – santahopar Jul 26 '20 at 03:52
  • Fair enough. Run the code snippet, or test with regex101.com, and you can see that just `Hello` is a no match – Peter Thoeny Jul 27 '20 at 04:49
  • After your last edit yes, it does satisfy all the requirements, which is why I have voted up your answer anyways. But since "The fourth bird's" answer was correct from the very beginning, I chose his as the solution. I think it's only fair, don't you agee? – santahopar Jul 27 '20 at 20:49
1

You could use

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

Explanation

  • ^ Start of string
  • (?!Hello\b) Negative lookahead, assert what is directly to thte right is not Hello
  • \S Match a non whitespace char
  • (?:.*\S)? Optionally match 0+ times any char except a newline and a non whitespace char
  • $ End of string

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70