javiut
javiut

Reputation: 193

Java regex emptyString only numbers and . or , numbers required

I am trying to develop a simple REGEX in Java without success so far. Here is what I am trying to accomplish.

  1. An empty String is not accepted. At least one group of numbers is required in the left side or the right side of the separator digit. 13. is accepted; .13 is accepted as well.

  2. The separator might be . or ,.

  3. Only one or zero character separator appearance is allowed.

  4. A separator character alone without numbers is not allowed. For example, . is not allowed alone.

The main problem I am facing is how I can make the separator optional. I am trying this:

private final Pattern pattern = Pattern.compile("^[0-9]*.?|,?[0-9]*$");

Here i am trying to specify a list of numbers at the start of the string which are optional. Later, I specify a separator character, which may be . or , which are optional as well. Later, the right side, also a string of numbers, and also optional. Here is the problem: only a . or a empty string is matching.

Here is my code:

    Matcher matcher = clazz.pattern.matcher(input);// a empty string should not be matching
    System.out.println(input+" "+matcher.matches());
    input="a";
    matcher = clazz.pattern.matcher(input);//true how can i fix this letter should not be allowed....
    System.out.println(input+" "+matcher.matches());
    input="13.";
    matcher = clazz.pattern.matcher(input);//returns true is OK.
    System.out.println(input+" "+matcher.matches());        
    input=".13";
    matcher = clazz.pattern.matcher(input);//returning false why is this if . is a character separator allowed.
    System.out.println(input+" "+matcher.matches());
    input="13.,";
    matcher = clazz.pattern.matcher(input);//false is OK only one separator might be specify.
    System.out.println(input+" "+matcher.matches());        
    input=",.13";
    matcher = clazz.pattern.matcher(input);//false is OK only one separator might be specify.
    System.out.println(input+" "+matcher.matches());        
    input="13,";
    matcher = clazz.pattern.matcher(input);//true is OK only one separator might be specify. , in this case
    System.out.println(input+" "+matcher.matches());        
    input=",13";
    matcher = clazz.pattern.matcher(input);
    System.out.println(input+" "+matcher.matches());//true is ok the separator is , in this case
    input="1131313133";
    matcher = clazz.pattern.matcher(input);
    System.out.println(input+" "+matcher.matches());//true only numbers        
    input="113.1313.133.13";
    matcher = clazz.pattern.matcher(input);
    System.out.println(input+" "+matcher.matches());//false is OK only one appeareance of the separator is allowed.        
    input="1131313133.132313";
    matcher = clazz.pattern.matcher(input);
    System.out.println(input+" "+matcher.matches());//false why is this... please help
    input=".";
    matcher = clazz.pattern.matcher(input);
    System.out.println(input+" "+matcher.matches()); //true how can i fix this i need a left or right sequence of numbers.       
    input=".....";
    matcher = clazz.pattern.matcher(input);
    System.out.println(input+" "+matcher.matches());//false is OK
    input=",,,,,,,";
    matcher = clazz.pattern.matcher(input);
    System.out.println(input+" "+matcher.matches());//false is OK

Upvotes: 0

Views: 4019

Answers (1)

Bernhard Barker
Bernhard Barker

Reputation: 55649

One problem, among others, is that, because of operator precedence

^[0-9]*.?|,?[0-9]*$

is basically the same as

(^[0-9]*.?)|(,?[0-9]*$)

So digits.digits won't match.

And [0-9]* means zero or more, so just . will match.

Also note that ^ and $ aren't actually required in this case, it's more for when you're using find() or similar.

What you want is probably something more like:

[0-9]+[.,]?|[0-9]*[.,][0-9]+

[.,] means either . or ,.
[0-9]+ means one or more digits.
Seems your familiar with the other syntax.

Reference.

EDIT:

Also note that for numbers you may not want 00000 to match, although not stated in the requirements. So something like:

(0|[1-9][0-9]*)[.,]?|(0|[1-9][0-9]*)?[.,][0-9]+

The above would match either a 0 or any number that doesn't start with a zero to the left of the optional separator (or nothing if there's something to the right of the separator).

Upvotes: 3

Related Questions