0

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
4

1 に答える 1

3

とりわけ、1つの問題は、演算子の優先順位のためです。

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

基本的には

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

だからdigits.digits一致しません。

そして[0-9]*、ゼロ以上を意味するので、ちょうど.一致します。

また、この場合は実際には必須ではないことに注意してください。これは、使用している場合などに適して^います。$find()

あなたが欲しいものはおそらくもっと似たものです:

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

[.,]またはのいずれ.かを意味します,1桁以上
[0-9]+を意味します。 他の構文に精通しているようです。

参照

編集:

00000また、要件には記載されていませんが、番号については一致させたくない場合があることに注意してください。だから次のようなもの:

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

上記は0、オプションの区切り文字の左側にあるゼロで始まらない(または区切り文字の右側に何かがある場合は何もない)任意の数値に一致します。

于 2013-03-15T16:01:56.323 に答える