5

我需要使这样的数字有效的正则表达式:

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

这些无效:

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

我尝试了这个正则表达式的不同变体:

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

测试代码:

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);
        }
    }
}

}

当控制台为空时,任务完成。现在我有这样的问题:

有效号码被检测为无效:1,233

有效号码被检测为无效:1,233,456.34

有效号码被检测为无效:1,345,234,122,123

无效号码被检测为有效:1。

问候。对不起大号。

更新。感谢Noob UnChained,我已经发展到这个正则表达式:

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

现在问题更少了:

有效号码被检测为无效:1,233,456.34

有效号码被检测为无效:.34

更新。

正则表达式:

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

问题:

无效号码被检测为有效:1。

完成的

最终结果是:

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

自上次更新以来的变化是:([.][\d]+)?

加上在这个块中,当用户输入数字然后放点但后面什么都没有时,这是不可能的情况。

新更新:在最后一个块中添加了 2 个“\”以避免输入逗号或其他符号而不是点。

新更新:感谢user2266098nhahtdh

user2266098 将我的注意力集中在“0.1”数字的未知问题上,并展示了将“|0”添加到第二个块的解决方案。但是他的正则表达式在我的数据中不能正确使用“+”和“-”(因为“()”而不是“[]”)。而且我不喜欢量词“{0,}”而不是“*”,因为它的大小。

nhahtdh 将我的注意力集中在空字符串的未知问题上,并用“(?!$)”显示了解决方案。

谢谢大家!

更新

本案有新的条件:

我需要使这样的数字有效的正则表达式:

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

这些无效:

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

我仍然无法获得“完美”的正则表达式 =)

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

给出:无效数字被检测为有效:0,123

4

2 回答 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

于 2013-04-16T16:28:53.407 回答
3

这个适用于我的所有测试数据:

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

顺便说一句,我希望您也希望 0.1 成为匹配项,但您的正则表达式不适用于它。我无法发表评论,所以我在答案中写了这个

于 2013-04-16T16:50:14.840 回答