0

到目前为止,我正在尝试用 Java 开发一个简单的正则表达式,但没有成功。这是我想要完成的。

  1. 不接受空字符串。分隔数字的左侧或右侧至少需要一组数字。13.被接受;.13也被接受。

  2. 分隔符可能是.,

  3. 只允许出现一个或零个字符分隔符。

  4. 不允许单独使用不带数字的分隔符。例如,.不允许单独使用。

我面临的主要问题是如何使分隔符成为可选的。我正在尝试这个:

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

在这里,我试图在字符串的开头指定一个数字列表,这些数字是可选的。稍后,我指定了一个分隔符,它可以是可选的,.也可以,是可选的。后来右边,也是一串数字,也是可选的。这是问题所在:只有一个.或一个空字符串匹配。

这是我的代码:

    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

其中一个问题是,由于运算符优先级

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

基本上是一样的

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

所以digits.digits不会匹配。

并且[0-9]*意味着零或更多,所以.会匹配。

另请注意,^$这种情况下实际上并不需要它,更多的是在您使用find()或类似时使用。

你想要的可能更像是:

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

[.,]表示要么.要么,
[0-9]+表示一位或多位数字。
似乎您熟悉其他语法。

参考

编辑:

另请注意,对于您可能不想00000匹配的数字,尽管要求中没有说明。所以像:

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

以上将匹配0可选分隔符左侧不以零开头的 a 或任何数字(如果分隔符右侧有某些内容,则不匹配)。

于 2013-03-15T16:01:56.323 回答