• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 23928
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: 23928
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: 67682
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.
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic