• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 23958
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: 23958
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
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not advanced. Moved.
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic