5

I need to make a regular expression with such numbers valid:

 "+1", "1.0", "1,233", "1,233,456.34", "-1", ".34", "1,345,234,122,123"

and these invalid:

 "++1", "1.0.0", "1,23,3", "+-1233456.34", "002", "1.", "a1", "1,,2", "1 2", "1,2", ",2".

I've tried different variants of this regexp:

 "[\\+\\-]?[1-9]{0,3}([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\.][\\d]*)?"

Code for testing:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class REGEX {
private static final String REGEX = "[\\+\\-]?[1-9]{0,3}([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\.][\\d]*)?";

private static String[] validNumbers = { "+1", "1.0", "1,233",
        "1,233,456.34", "-1", ".34", "1,345,234,122,123" };

private static String[] invalidNumbers = { "++1", "1.0.0", "1,23,3",
        "+-1233456.34", "002", "1.", "a1", "1,,2", "1 2", "1,2", ",2" };

public static void main(String[] args) {
    Pattern pattern = Pattern.compile(REGEX);

    for (String number : validNumbers) {
        Matcher matcher = pattern.matcher(number);
        if (!matcher.matches()) {
            System.out.println("Valid number is detected as invalid: "
                    + number);
        }
    }
    for (String number : invalidNumbers) {
        Matcher matcher = pattern.matcher(number);
        if (matcher.matches()) {
            System.out.println("Invalid number is detected as valid: "
                    + number);
        }
    }
}

}

When the console will be empty then the task is done. Now I've such problems:

Valid number is detected as invalid: 1,233

Valid number is detected as invalid: 1,233,456.34

Valid number is detected as invalid: 1,345,234,122,123

Invalid number is detected as valid: 1.

Regards. Sorry for big size.

Update. Thanks to Noob UnChained, I've progressed to this regexp:

^([\\+\\-]?[1-9]\\d{0,2})*(\\,\\d{3})*([\\.][\\d*])?$

and now there is fewer problems:

Valid number is detected as invalid: 1,233,456.34

Valid number is detected as invalid: .34

Update.

regexp:

"([\\+\\-]?[1-9]\\d{0,2})*(\\,\\d{3})*([.][\\d]*)?"

problems:

Invalid number is detected as valid: 1.

FINISHED

The final result is:

"(?!$)[\\+-]?([1-9]\\d{0,2}|0)?(\\,\\d{3})*(\\.\\d+)?"

Change since the last update is:([.][\d]+)?

plus in this block makes impossible situation when user inputs number then puts dot but nothing after it.

New update: added 2 "\" into last block to avoid entering comma or other symbol instead of dot.

New update: Thanks to user2266098 and nhahtdh.

user2266098 pointed my attention on the uncharted problem of "0.1" number and showed solution with adding "|0" to the second block. But his regexp doesn't work correctly with "+" and "-" for my data (because of "()" instead of "[]"). And I don't like the quantifier "{0,}" instead of "*" because of it's size.

nhahtdh pointed my attention on the uncharted problem of empty string and showed solution with "(?!$)".

Thanks to everyone!

Update

There are new conditions for this case:

I need to make a regular expression with such numbers valid:

"+1", "1.0", "1,233", "1,233,456.34", "-1", ".34", "1,345,234,122,123", "0.1"

and these invalid:

"++1", "1.0.0", "1,23,3", "+-1233456.34", "002", "1.", "a1", "1,,2", "1 2", "1,2", ",2", "", "0,123"

I still can't get "perfect" regexp =)

"(?!$)[\\+-]?([1-9]\\d{0,2}|0)?(\\,\\d{3})*(\\.\\d+)?"

gives: Invalid number is detected as valid: 0,123

8
  • 1
    This may be of help : stackoverflow.com/questions/2811031/… Commented Apr 16, 2013 at 15:55
  • 1
    You can replace ([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})?([\\d]{3}[\\,]{1})? with (\\d{3}\\,){0,5} (they're the same). Commented Apr 16, 2013 at 15:59
  • Do you need to do this with a regex? There are methods that you can call to do this. Commented Apr 16, 2013 at 16:01
  • It's my task, so I may not use other ways =( Commented Apr 16, 2013 at 16:06
  • @YaroslavSelivanov You are very close to achieve. For .34 replace ^([\\+\\-]? with ^([+-\\.]? Commented Apr 16, 2013 at 16:19

2 Answers 2

4

FINISHED

The final result is:

"(?!$)[\\+-]?([1-9]\\d{0,2}|0)?(\\,\\d{3})*(\\.\\d+)?"

Change since the last update is:([.][\d]+)?

plus in this block makes impossible situation when user inputs number then puts dot but nothing after it.

New update: added 2 "\" into last block to avoid entering comma or other symbol instead of dot.

New update: Thanks to user2266098 and nhahtdh.

user2266098 pointed my attention on the uncharted problem of "0.1" number and showed solution with adding "|0" to the second block. But his regexp doesn't work correctly with "+" and "-" for my data. And I don't like the quantifier "{0,}" instead of "*" because of it's size.

nhahtdh pointed my attention on the uncharted problem of empty string and showed solution with "(?!$)".

Thanks to everyone!

Update

There are new conditions for this case:

I need to make a regular expression with such numbers valid:

"+1", "1.0", "1,233", "1,233,456.34", "-1", ".34", "1,345,234,122,123", "0.1"

and these invalid:

"++1", "1.0.0", "1,23,3", "+-1233456.34", "002", "1.", "a1", "1,,2", "1 2", "1,2", ",2", "", "0,123"

I still can't get "perfect" regexp =)

"(?!$)[\\+-]?([1-9]\\d{0,2}|0)?(\\,\\d{3})*(\\.\\d+)?"

gives: Invalid number is detected as valid: 0,123

Sign up to request clarification or add additional context in comments.

Comments

3

This one works for me on all your test data:

^(\\+|-)?([1-9]\\d{0,2}|0)?(,\\d{3}){0,}(\\.\\d+)?

btw I expect you also want 0.1 to be a match, but your regexp doesn't work on it. I can't comment, so I'm writing this in an answer

11 Comments

Your regex will allow empty string.
@user2266098 Did you lost one symbol in the quantifier? ^(\\+|-)?([1-9]\\d{0,2}|0)?(,\\d{3})**{0,}**(\\.\\d+)?
@nhahtdh Is there any option to avoid this?
Yep... will allow empty string. Anyway the question was not about perfect regexp, but sadly just to pass the homework test. Yaroslav: I didn't miss the quantifier. There can be infinite number of (\,\d{3}) groups I think.
@YaroslavSelivanov: I haven't looked at your question in details, so I don't know. A crude and dirty fix is to put an assertion (?!$) at the beginning.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.