I am trying to develop a simple REGEX in Java without success so far. Here is what I am trying to accomplish.
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.The separator might be
.
or,
.Only one or zero character separator appearance is allowed.
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