• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Regular Expressions

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

We need to write a regular expression which can accept a list of alphanumeric characters separated by single commas. The list can't start with the , but can end with a ,


Length of the characters will be from 1 to 9. So, considering following examples and their outcomes:



I have written the following regular expression:



Here, it is working fine for all the cases EXCEPT ONE i.e. it always expects the , in the end of the string. But, as per my requirement the string can end with a , or may not end with a , as well. I tried putting ,? but then it starts accepting more than 9 characters since it accepts 0 or 1 , between two entries.

Please suggest how to resolve this issue. Thanks in advance.

Thanks,
Vaibhav Garg
 
author & internet detective
Posts: 42134
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have to use a regular expression for the whole thing? It would be easier to write if (s.charAt(0) != ',' && s.matches(yourRegex) rather than complicating the regular expression.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just need to replace the ',' in the regular expression so that it accepts as terminator a comma OR end of line ie '(,|$)' .
 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am able to write down the regular expression for this:


Thanks all for your inputs.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is much more complicated than the approach I suggested!
 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I agree Richard Tookey. Your approach is much more simpler. Thanks a lot for your inputs.
 
reply
    Bookmark Topic Watch Topic
  • New Topic