How to create a regular expression for any string up to a maximum length?
-
This is what I tried to allow any string up to a maximum of 32 characters, but it does not work:
QRegularExpression regExp("^.{,32}$");
In my same code this expression worked fine:
QRegularExpression regExp("^[0-9]{0,8}$");
-
This is what I tried to allow any string up to a maximum of 32 characters, but it does not work:
QRegularExpression regExp("^.{,32}$");
In my same code this expression worked fine:
QRegularExpression regExp("^[0-9]{0,8}$");
It should already work if you change the pattern to
"^.{0,32}$"
Test: https://regex101.com/r/PN7hSo/1
@raven-worx: I tried your pattern in the online tester but it did not match :(
-
It should already work if you change the pattern to
"^.{0,32}$"
Test: https://regex101.com/r/PN7hSo/1
@raven-worx: I tried your pattern in the online tester but it did not match :(
@aha_1980
odd, seems like "^[]" has some special meaning?
It works when [] is replaced with () though -
@aha_1980
odd, seems like "^[]" has some special meaning?
It works when [] is replaced with () though@raven-worx said in How to create a regular expression for any string up to a maximum length?:
@aha_1980
odd, seems like "^[]" has some special meaning?Don't ask me - in theory it should work :)
It works when [] is replaced with () though
Yeah, that creates a capture group around '.' and is needed if you want to re-use the result. Otherwise you can just use '.'
-
@raven-worx said in How to create a regular expression for any string up to a maximum length?:
@aha_1980
odd, seems like "^[]" has some special meaning?Don't ask me - in theory it should work :)
It works when [] is replaced with () though
Yeah, that creates a capture group around '.' and is needed if you want to re-use the result. Otherwise you can just use '.'