PDA

View Full Version : QRegExp for strings with numbers



roseicollis
11th March 2015, 11:30
Hi!

I need a regexp which accepts a string (maximum 30 characters/Slots) with:

- letters from A to Z (not Á or Ñ or Ë...)
- numbers
- spaces
- signs: . , -

No matter the order so valid examples are :

- Hello how are you
- Hi. My -name- is ... mm

I was trying with that but it just accepts letters and numbers:


QRegExp rx7("[A-Z0-9]{0,30}");
QValidator *validator7 = new QRegExpValidator(rx7, this);
myLineEdit->setValidator(validator7);


Thank you so much

wysota
11th March 2015, 11:43
It only accepts letters and numbers because that's what your regexp contains. You have to put the remaining allowed characters in the selector as well.

roseicollis
11th March 2015, 11:51
Hi wysota,

Yes I knwo but I don't know how to put dot, coma, spaces and - too. Tried also with that but it accepts too !"··%$&%((/=)**^¨`+

QRegExp rx7("[A-Z.- 0-9]{0,30}");

anda_skoa
11th March 2015, 11:58
That's because an unescaped dot in a regular expression means "match anything".

Cheers,
_

Lesiok
11th March 2015, 11:59
Read about "special characters in regexp". It should looks like :
QRegExp rx7("[A-Z\\.\\- 0-9]{0,30}");Double backslash derived from the principles of code C / C ++.

roseicollis
11th March 2015, 12:11
wow didn't know that about dot anda_skoa

Thanks Lesiok for the clear example :) You all helped me so much in a moment. :)