|
|
Need an expression to validate numeric digit is sequential and sort ascending(desc) ,such as 123456 , 456789 are valid .
Last post 10-15-2008, 5:13 AM by sparky. 22 replies.
-
10-06-2008, 9:58 PM |
-
sparky
-
-
-
Joined on 10-07-2008
-
-
Posts 18
-
-
|
Need an expression to validate numeric digit is sequential and sort ascending(desc) ,such as 123456 , 456789 are valid .
Hello , I want to validate the numeric digit is sequential and sort ascending (desc) , such as 123456 , 456789 are valid . 876543 ,54321 are also valid , Is anybody who can help me solve the problem ? thanks for your kindly help ^O^ my MSN: sparkyhuang520@hotmail.com Email : jianlin.huang@hp.com
|
|
-
10-07-2008, 2:08 AM |
-
ddrudik
-
-
-
Joined on 05-24-2007
-
USA
-
Posts 2,079
-
-
|
Re: Need an expression to validate numeric digit is sequential and sort ascending(desc) ,such as 123456 , 456789 are valid .
The way to do this with regex is to put all of your combinations together using alternation: ^(?:123456|234567|345678|etc...)$ Although this is easier to accomplish with code.
|
|
-
10-07-2008, 2:28 AM |
-
mash
-
-
-
Joined on 04-14-2005
-
Birmingham, AL
-
Posts 1,959
-
-
|
Re: Need an expression to validate numeric digit is sequential and sort ascending(desc) ,such as 123456 , 456789 are valid .
Try
(?=\d+)(?:(?:1?2?3?4?5?6?7?8?9?)|(?:9?8?7?6?5?4?3?2?1?))(?<=\d)\b
Michael "In theory, theory and practice are the same. In practice, they are not." Albert Einstein
|
|
-
10-08-2008, 12:10 AM |
|
|
Re: Need an expression to validate numeric digit is sequential and sort ascending(desc) ,such as 123456 , 456789 are valid .
Michael, Your pattern will find ascending/descending digit sequences but will also include nonsequential digits such as 1345 Not that I have any alternatives to offer, but this does not quite meet the OP's stated requirement. Susan
|
|
-
10-08-2008, 3:54 AM |
-
sparky
-
-
-
Joined on 10-07-2008
-
-
Posts 18
-
-
|
Re: Need an expression to validate numeric digit is sequential and sort ascending(desc) ,such as 123456 , 456789 are valid .
Michael, Your pattern will find ascending/descending digit sequences but will also include nonsequential digits such as 1345 or 1357 So it does not quite meet the my stated requirement. BTW, Need an expression to validate such a pattern : ABCABDABE (Note: A B C D E are different numeric digits , C.D.E should be sort ascending and sequential ), example :123124125 is valid , 352353354 is also valid . Thanks anyway ,hope you can solve the problem , Best regards, Sparky
|
|
-
-
10-08-2008, 1:53 PM |
-
mash
-
-
-
Joined on 04-14-2005
-
Birmingham, AL
-
Posts 1,959
-
-
|
Re: Need an expression to validate numeric digit is sequential and sort ascending(desc) ,such as 123456 , 456789 are valid .
ddrudik:That does not appear to be something regex can handle alone, code would be required.
That was my initial thought too. I thought I could work around it but I didn't bother with any negative testing. Thank for keeping me honest guys.  However I had another thought. But I'm have serious computer issues today so once I get it written I'll try to post it.
Michael "In theory, theory and practice are the same. In practice, they are not." Albert Einstein
|
|
-
10-08-2008, 2:14 PM |
-
mash
-
-
-
Joined on 04-14-2005
-
Birmingham, AL
-
Posts 1,959
-
-
|
Re: Need an expression to validate numeric digit is sequential and sort ascending(desc) ,such as 123456 , 456789 are valid .
mash:
ddrudik:That does not appear to be something regex can handle alone, code would be required.
That was my initial thought too. I thought I could work around it but I didn't bother with any negative testing. Thank for keeping me honest guys. 
However I had another thought. But I'm have serious computer issues today so once I get it written I'll try to post it.
Ok, not that I recommend using this but just to see if it can be done.
Options use: IgnoreWhitespace, Explicit Capture
(\b|(?<!\d))(
(1(?(\d)2(?(\d)3(?(\d)4(?(\d)5(?(\d)6(?(\d)7(?(\d)8(?(\d)9|\b)|\b)|\b)|\b)|\b)|\b)|\b)|\b)|
2(?(\d)3(?(\d)4(?(\d)5(?(\d)6(?(\d)7(?(\d)8(?(\d)9|\b)|\b)|\b)|\b)|\b)|\b)|\b)|
3(?(\d)4(?(\d)5(?(\d)6(?(\d)7(?(\d)8(?(\d)9|\b)|\b)|\b)|\b)|\b)|\b)|
4(?(\d)5(?(\d)6(?(\d)7(?(\d)8(?(\d)9|\b)|\b)|\b)|\b)|\b)|
5(?(\d)6(?(\d)7(?(\d)8(?(\d)9|\b)|\b)|\b)|\b)|
6(?(\d)7(?(\d)8(?(\d)9|\b)|\b)|\b)|
7(?(\d)8(?(\d)9|\b)|\b)|
8(?(\d)9|\b)|
9)
|(
9(?(\d)8(?(\d)7(?(\d)6(?(\d)5(?(\d)4(?(\d)3(?(\d)2(?(\d)1|\b)|\b)|\b)|\b)|\b)|\b)|\b)|\b)|
8(?(\d)7(?(\d)6(?(\d)5(?(\d)4(?(\d)3(?(\d)2(?(\d)1|\b)|\b)|\b)|\b)|\b)|\b)|\b)|
7(?(\d)6(?(\d)5(?(\d)4(?(\d)3(?(\d)2(?(\d)1|\b)|\b)|\b)|\b)|\b)|\b)|
6(?(\d)5(?(\d)4(?(\d)3(?(\d)2(?(\d)1|\b)|\b)|\b)|\b)|\b)|
5(?(\d)4(?(\d)3(?(\d)2(?(\d)1|\b)|\b)|\b)|\b)|
4(?(\d)3(?(\d)2(?(\d)1|\b)|\b)|\b)|
3(?(\d)2(?(\d)1|\b)|\b)|
2(?(\d)1|\b)|
1
)
)(\b|(?!\d))
Tested in Expresso (when it wasn't crashing) with the values
1234
1345
1357
34
256
234
983
987
4 matches
Michael "In theory, theory and practice are the same. In practice, they are not." Albert Einstein
|
|
-
10-08-2008, 4:09 PM |
-
prometheuzz
-
-
-
Joined on 04-28-2008
-
-
Posts 659
-
-
|
Re: Need an expression to validate numeric digit is sequential and sort ascending(desc) ,such as 123456 , 456789 are valid .
Ah, a "curiosity-cabinet-regex", I like those! ; ) Here's mine:
^(
(0(?=1|$))?((?<=0|^)1(?=2|$))?((?<=1|^)2(?=3|$))?((?<=2|^)3(?=4|$))?((?<=3|^)4(?=5|$))?((?<=4|^)5(?=6|$))?((?<=5|^)6(?=7|$))?((?<=6|^)7(?=8|$))?((?<=7|^)8(?=9|$))?((?<=8|^)9)? |
(9(?=8|$))?((?<=9|^)8(?=7|$))?((?<=8|^)7(?=6|$))?((?<=7|^)6(?=5|$))?((?<=6|^)5(?=4|$))?((?<=5|^)4(?=3|$))?((?<=4|^)3(?=2|$))?((?<=3|^)2(?=1|$))?((?<=2|^)1(?=0|$))?((?<=1|^)0)? )$
|
|
-
10-08-2008, 4:13 PM |
-
mash
-
-
-
Joined on 04-14-2005
-
Birmingham, AL
-
Posts 1,959
-
-
|
Re: Need an expression to validate numeric digit is sequential and sort ascending(desc) ,such as 123456 , 456789 are valid .
sparky:BTW, Need an expression to validate such a pattern : ABCABDABE (Note: A B C D E are different numeric digits , C.D.E should be sort ascending and sequential ), example :123124125 is valid , 352353354 is also valid . Thanks anyway ,hope you can solve the problem , Best regards, Sparky
I hope you understand that a regex has no concept of numerical value.Everything is just a sequence of characters. http://regexadvice.com/blogs/mash/archive/2007/06/01/Are-you-ready-for-regex_3F00_.aspx
Michael "In theory, theory and practice are the same. In practice, they are not." Albert Einstein
|
|
-
10-08-2008, 5:05 PM |
-
ddrudik
-
-
-
Joined on 05-24-2007
-
USA
-
Posts 2,079
-
-
|
Re: Need an expression to validate numeric digit is sequential and sort ascending(desc) ,such as 123456 , 456789 are valid .
prometheuzz: Ah, a "curiosity-cabinet-regex", I like those! ; ) Here's mine:
^( (0(?=1|$))?((?<=0|^)1(?=2|$))?((?<=1|^)2(?=3|$))?((?<=2|^)3(?=4|$))?((?<=3|^)4(?=5|$))?((?<=4|^)5(?=6|$))?((?<=5|^)6(?=7|$))?((?<=6|^)7(?=8|$))?((?<=7|^)8(?=9|$))?((?<=8|^)9)? | (9(?=8|$))?((?<=9|^)8(?=7|$))?((?<=8|^)7(?=6|$))?((?<=7|^)6(?=5|$))?((?<=6|^)5(?=4|$))?((?<=5|^)4(?=3|$))?((?<=4|^)3(?=2|$))?((?<=3|^)2(?=1|$))?((?<=2|^)1(?=0|$))?((?<=1|^)0)? )$
Not that it changes the pattern any, but here's that without the whitespace and extra capture groups: ^(?:(?:0(?=1|$))?(?:(?<=0|^)1(?=2|$))?(?:(?<=1|^)2(?=3|$))?(?:(?<=2|^)3(?=4|$))?(?:(?<=3|^)4(?=5|$))?(?:(?<=4|^)5(?=6|$))?(?:(?<=5|^)6(?=7|$))?(?:(?<=6|^)7(?=8|$))?(?:(?<=7|^)8(?=9|$))?(?:(?<=8|^)9)?|(?:9(?=8|$))?(?:(?<=9|^)8(?=7|$))?(?:(?<=8|^)7(?=6|$))?(?:(?<=7|^)6(?=5|$))?(?:(?<=6|^)5(?=4|$))?(?:(?<=5|^)4(?=3|$))?(?:(?<=4|^)3(?=2|$))?(?:(?<=3|^)2(?=1|$))?(?:(?<=2|^)1(?=0|$))?(?:(?<=1|^)0)?)$ prometheuzz, excellent pattern.
|
|
-
10-09-2008, 1:04 AM |
-
sparky
-
-
-
Joined on 10-07-2008
-
-
Posts 18
-
-
|
Re: Need an expression to validate ABCABDABE , (A B C D E are numeric digit , C D E must be serial and sort ascending) , such as 134135136 is valid ...
Perfect ,thanks, but how to validate ABCABDABE , (A B C D E are numeric digit , C D E must be serial and sort ascending) , such as 134135136 is valid ...
|
|
-
10-09-2008, 1:16 AM |
-
ddrudik
-
-
-
Joined on 05-24-2007
-
USA
-
Posts 2,079
-
-
|
Re: Need an expression to validate ABCABDABE , (A B C D E are numeric digit , C D E must be serial and sort ascending) , such as 134135136 is valid ...
How does '13' fit into your requirement, for example could there be any A B digit pair before C D E in your example? And, how many total digits do you require in the string? Could the string be A C A D A E instead etc.? Note that your real question doesn't seem much like the original question that started this thread. Also, what platform are you using this regex on?
|
|
-
10-09-2008, 1:59 AM |
-
sparky
-
-
-
Joined on 10-07-2008
-
-
Posts 18
-
-
|
Re: Need an expression to validate ABCABDABE , (A B C D E are numeric digit , C D E must be serial and sort ascending) , such as 134135136 is valid ...
Dear ddrudik : I use the regex on java under windows platform . I have 2 questions as follow: 1. ABCABDABE (A B C D E are numeric digit , C D E must be serial and sort ascending), example: 245246247 is valid ... 2.how to validate the location in a string ,example: I want to validate the position of '6' in the string "0134362379", and then compare with a digit(eg.5) ,if it more than 5 ,return valid ,else return invalid ... Could you add my MSN : sparkyhuang520@hotmail.com Many thanks and Best regards, Sparky
|
|
-
10-09-2008, 3:58 AM |
-
ddrudik
-
-
-
Joined on 05-24-2007
-
USA
-
Posts 2,079
-
-
|
Re: Need an expression to validate ABCABDABE , (A B C D E are numeric digit , C D E must be serial and sort ascending) , such as 134135136 is valid ...
To receive e-mail updates on comments to questions please use the Enable Email Subscriptions button near the top of this page. Let me take the second question first by asking what you mean by validating the position of 6? For example, do you mean such that with your example, 6 is at substring index 5 and that's what you are testing for? If 6 is located somewhere before or after 5 as well as at position 5 does that fail your test? As with your first question, you could do these things with regex but since you have a programming platform that allows for less overhead with using simple string functions why bother with regex? For #1, to do it easily with code, get the three substring values for positions 2,5,8 and test that each one is the previous + 1. If there are additional tests on the 24 sections of the string we need to know those. Now I am sure that a regex pattern can be constructed by someone to satisfy this requirement. Before others work on the pattern please specify what rules are involved with the 24 sections of the pattern, such as they need to be identical (assumed) and/or they need to be certain digits etc. Also I assume the number you are validating is always 9 digits in length, if not please give more details. If you still want to use regex for the #1 (assuming the 24 in your question could be any two digits): Pattern re = Pattern.compile("^(\\d\\d)(?:1\\1[2]\\1[3]|2\\1[3]\\1[4]|3\\1[4]\\1[5]|4\\1[5]\\1[6]|5\\1[6]\\1[7]|6\\1[7]\\1[8]|7\\1[8]\\1[9])$");
|
|
Page 1 of 2 (23 items)
1
|
|
|