PDA

View Full Version : QRegExp Not Repeated More Than Four Times.



mandlakoteswar2011
21st September 2015, 09:06
Here is the Regular Expression.

QRegExp rx("^(?:[7-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9])$");

Above Regular Expression I Dont Want Repeat More Than Four Times In LineEdit.

Ex:7 22222 2222

Kindly Do NeedFul..

ChrisW67
21st September 2015, 12:15
What does the regular expression have to do with a line edit?
What don't you want to repeat? The expression will match a 7 or 9 followed by up to nine other digits.

mandlakoteswar2011
21st September 2015, 14:34
7 22222 222

The Above Experisson it Wont Take "2"'s more than four times.

plz give me the Regular Experssion Syntax.

anda_skoa
21st September 2015, 15:17
The Above Experisson it Wont Take "2"'s more than four times.

As Chris67 said: your expression accecpts up to 9 digits after the first.
If you only want to accept 4, then don't add the remaining 5 options.

Cheers,
_

d_stranz
21st September 2015, 18:49
QRegExp rx("^(?:[7-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9])$");

As I read your regular expression, it says:

Match:

from the start of the string ("^")
the sub-expression: ("(?:")
zero or one of 7, 8, or 9 ("[7-9]?")
followed by zero or one of 0 through 9, up to eight times ("[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?")
followed by a single digit 0 through 9 ("[0-9]")
and this must be the entire string (")$")


So your regex will match the following:


Any string containing only a single digit 0 - 9 (including strings containing only "7", "8", or "9")
Any string starting with 7, 8, or 9 and ending with any single digit 0 - 9
Any string starting with 7, 8, or 9 followed by up to eight more digits 0 - 9, ending with one more 0 - 9
Any string containing one through nine digits between 0 and 9


For example:


"0" (or "1", "2", ... "9")
"70" (or "71", "72", ... "99")
"70" (or "701", "7012", "7013", ... "7012345678")
"0" (or "01", "012", "0123", ... "012345678"


Note that it will not match "7 22222 222" since this contains embedded whitespace. It will match "722222222".

Edit: So, your regex will match any string of one through eight digits between "0" and "99999999" and also any string of one through nine digits that starts with a "7", "8", or "9".

ChrisW67
21st September 2015, 22:25
Nice explanation d_stranz. Way more effort than I put in (no way i was going type type that on my tablet ;) ). I hope it is not lost on the OP.

jefftee
22nd September 2015, 04:29
QRegExp rx("^(?:[7-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9])$");
If you are attempting to require that the first digit is 7-9, your regex is not correct as you have said zero or one (?) digit 7-9 is required. If you want to require that the regex matches a number that starts with 7-9 and has a max of 9 other digits 0-9, then the regex should be:


QRegExp rx("^(?:[7-9][0-9]{1,9})$");

The above will match a digit between 7-9 followed by up to a minimum of 1 and maximum of 9 more digits 0-9. Additionally, since you're using a non-capturing group, which consists is the entire regex expression, you can likely just remove the grouping altogether.

d_stranz
22nd September 2015, 21:32
Additionally, since you're using a non-capturing group, which consists is the entire regex expression, you can likely just remove the grouping altogether.

I thought about mentioning that as well as posting the simplified expression you gave. But since I have no idea what the OP was actually trying to match, I just left it alone.

Perhaps the OP can respond with examples of what he does and does not want to match, then we can help with a better regex.

Spent a lot of time years ago writing lex and yacc code to parse specialty "little languages" (as Jon Bentley calls them), so got pretty good at crafting regular expressions.

jefftee
22nd September 2015, 22:34
Spent a lot of time years ago writing lex and yacc code to parse specialty "little languages" (as Jon Bentley calls them), so got pretty good at crafting regular expressions.
lex, yacc, regular expressions, and many other CS concepts are a black/lost art today for sure and as foreign to some as the courses I took way back in my undergrad days. Most of my CS background was theory based, data structures, algorithms, and of course programming language based, etc. Not sure what the CS curriculum in schools are all about nowadays, but I'm sure they're rife with the web languages of the day, HTML, CSS, etc.

Often I find tortured uses for regular expressions when they're not really needed, or the opposite where a regex is a perfect solution that gets implemented in unmaintainable spaghetti code to parse stuff... The most valuable skill to have is to know when one is called for vs the other... :)