Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Easy Regular Expression question

 
Carlos Bonzilla
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a regular expression meeting the following requirements

* 1 or 2 digits
followed by
, (comma sign)
followed by
0,1,2 or 3 digits

I.e
2,777 is ok
2.777 is not ok
2,7777 is not ok

I tried this but it does not work.
[0-9]{1,2},[0-9]{0,3} ( allows everything but comma as separator

Best regards
/Carlos
 
Yunnan Zhou
Ranch Hand
Posts: 31
C++ Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I test your expression ,it's ok. what's wrong?
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your posted regex works fine for me.To get better help sooner, post a SSCCE that clearly demonstrates your problem. And wrap the code in code tags so it retains its readability.
 
Carlos Bonzilla
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Your posted regex works fine for me.To get better help sooner, post a SSCCE that clearly demonstrates your problem. And wrap the code in code tags so it retains its readability.


Thanks for the tip Darryl and thanks to you both for the help.

I may have been a bit unclear when describing the problem. Sry for that. But now :
* 2 evaluates to false (should be ok)
* 2, evaluates to true (should not be ok).
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because the regular expression and the previous requirements you posted do not match these desired results. Your requirements were to always have a comma after the first 1-2 digits, then have 0-3 more digits.

It appears the requirements are different:
- 1-2 digits.
- either nothing, or a comma followed by 1-3 digits.

That's two parts, the latter is divided into two sub parts: [0-9]{1,2}(,[0-9]{1,3})?
The ()? part makes everything between the () optional.
 
Carlos Bonzilla
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:That's because the regular expression and the previous requirements you posted do not match these desired results. Your requirements were to always have a comma after the first 1-2 digits, then have 0-3 more digits.

It appears the requirements are different:
- 1-2 digits.
- either nothing, or a comma followed by 1-3 digits.

That's two parts, the latter is divided into two sub parts: [0-9]{1,2}(,[0-9]{1,3})?
The ()? part makes everything between the () optional.


Fantastic ! Thanks for your help Rob. Greatly appreciated.

Best regards
/Carlos
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic