• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regular Expression - [0-9]{1}[[0-9]*[,]{1}]{1,14}

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to write a Regular Expression to match the string with the following criteria:

1. the string can only include digits and one and only one comma
2. the whole length of the string is less than or equal to 15
3. the comma can appear anywhere in the string but not at the first place of the string

For example, this string '12123451,13,' should not be matched.

Any advice will be appreciated. Thank you.
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Theoretically, it should possible to come up with a RE for what you ask, but it is going to be terribly long and complicated. I would suggest that you use a RE like so -

- and then check for the length of the matched string to be lesser than 15.

Also suggest you wait and watch for other gurus to chime in,
- Anand
 
Caly LeeAnn
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That doesn't restrict the whole length of the string to less than or equal to 15. This string "0123456789,0123456789" which its length is 21 passes.
 
Anand Hariharan
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have liked to think you'd read my post in entirety before responding.

- Anand
 
Caly LeeAnn
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anand,

Sorry about that.

Is it possible to restrict the length in the RE itself? Checking the length later is not first option for the condition.
 
Anand Hariharan
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is beginning to sound like homework. For whatever it is worth, here is a RE that I expect should work, but since I haven't tested it, I cannot say for certain. Use it at your own risk:



good luck,
- Anand
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Caly LeeAnn:

Is it possible to restrict the length in the RE itself? Checking the length later is not first option for the condition.



I believe Anand is correct here. The regex can't do both operations in a single pass.

However, why can't you check the length first? It's just a quick check, and if the length isn't valid, then you save the time to even check with the regex.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is beginning to sound like homework. For whatever it is worth, here is a RE that I expect should work, but since I haven't tested it, I cannot say for certain. Use it at your own risk:




One of the reasons that you want to use a single pass, is to save processing time. That new regex is ridiculously large, and checking the length is ridiculously easy....

Henry
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This can be achieved in one regex using lookahead or lookbehind. E.g,

"(?=.{1,15}$)\\d+(?:,\\d*)?"

(first look ahead and see that there are 1-15 characters before the end, then match the digits and comma)

or

"\\d+(?:,\\d*)?(?<=^.{1,15})"

(first match the digits and comma, then look back and check that the total length was 1-15)

These expressions assume that there could be a single comma, but it wasn't mandatory. If the comma is required, then use


"(?=.{1,15}$)\\d+,\\d*"

or

"\\d+,\\d*(?<=^.{1,15})"

Also, the initial requirements don't say whether a blank string is allowed or not. If it is allowed, you'll have to modify any of these solutions.
 
Anand Hariharan
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:

One of the reasons that you want to use a single pass, is to save processing time. That new regex is ridiculously large, and checking the length is ridiculously easy....
Henry



Couldn't agree more with you, Henry. The reason that the OP insisted that it had to be done in the RE is what got me to suspect that it might be homework. If she just wanted a solution, I think the first RE that I proposed (together with the length check) would be more than adequate.

Of course, you said "ridiculously large", I said "terribly long and complicated". You say Po-Tay-Toe, I say ....

- Anand
 
Anand Hariharan
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
This can be achieved in one regex using lookahead or lookbehind.
(...)



Awesome!

- Anand
 
Caly LeeAnn
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Jim! This is simple and straight forward and it's exactly what I need!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic