wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Easy Regular Expression question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Easy Regular Expression question" Watch "Easy Regular Expression question" New topic
Author

Easy Regular Expression question

Carlos Bonzilla
Greenhorn

Joined: May 03, 2011
Posts: 17
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

Joined: May 04, 2011
Posts: 31

I test your expression ,it's ok. what's wrong?


I'am a Chinese.I like Java.Hello.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Carlos Bonzilla
Greenhorn

Joined: May 03, 2011
Posts: 17
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

Joined: Oct 27, 2005
Posts: 19653
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Carlos Bonzilla
Greenhorn

Joined: May 03, 2011
Posts: 17
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Easy Regular Expression question
 
Similar Threads
Strings
xs:pattern meaning
regular expression validation script
Strings
regarding regular expression