如何编写在Java中匹配String和BigInteger的正则表达式?

huangapple go评论78阅读模式
英文:

How write regex for matches String and BigInteger in java?

问题

我尝试使用这个网站 https://www.freeformatter.com/java-regex-tester.html#ad-output,但我没有成功。

我的Java正则表达式:

(.*)\\w+=[0-9][0-9]*$(.*)

要测试的输入:

a = 800000000000000000000000

我的代码需要对 a = 800000000000000000000000 使用 data.matches("(.*)\\w+=[0-9][0-9]*$(.*)"),应返回 true

英文:

I tried to use this site https://www.freeformatter.com/java-regex-tester.html#ad-output. But I didn’t succeed.

My Java Regular Expression :

(.*)\\w+=[0-9][0-9]*$(.*)

Entry to test against :

a = 800000000000000000000000

My code needs to give true for data.matches("(.*)\\w+=[0-9][0-9]*$(.*)") of a = 800000000000000000000000.

答案1

得分: 2

按照以下方式进行操作

    public class Main {
        public static void main(String[] args) {
            String[] testStrs = { "a = 800000000000000000000000", "a = ABC800000000000000000000000",
                    "a = 800000000000000000000000ABC", "a = 12.45", "a = 800000000000ABC000000000000", "a = ABC",
                    "a = 900000000000000000000000", "a = -800000000000000000000000", "a = -900000000000000000000000" };
            for (String str : testStrs) {
                System.out.println(
                        str + " -> " + (str.matches("[A-Za-z]\\s+=\\s+[-]?\\d+") ? " 匹配。" : " 不匹配。"));
            }
        }
    }

输出:

a = 800000000000000000000000 ->  匹配。
a = ABC800000000000000000000000 ->  不匹配。
a = 800000000000000000000000ABC ->  不匹配。
a = 12.45 ->  不匹配。
a = 800000000000ABC000000000000 ->  不匹配。
a = ABC ->  不匹配。
a = 900000000000000000000000 ->  匹配。
a = -800000000000000000000000 ->  匹配。
a = -900000000000000000000000 ->  匹配。

解释:

  1. [A-Za-z] 表示字母
  2. \\s+ 表示一个或多个空格
  3. [-]? 表示可选的 - 符号
  4. \\d+ 表示数字
英文:

Do it as follows:

public class Main {
	public static void main(String[] args) {
		String[] testStrs = { "a = 800000000000000000000000", "a = ABC800000000000000000000000",
				"a = 800000000000000000000000ABC", "a = 12.45", "a = 800000000000ABC000000000000", "a = ABC",
				"a = 900000000000000000000000", "a = -800000000000000000000000", "a = -900000000000000000000000" };
		for (String str : testStrs) {
			System.out.println(
					str + " -> " + (str.matches("[A-Za-z]\\s+=\\s+[-]?\\d+") ? " matches." : " does not match."));
		}
	}
}

Output:

a = 800000000000000000000000 ->  matches.
a = ABC800000000000000000000000 ->  does not match.
a = 800000000000000000000000ABC ->  does not match.
a = 12.45 ->  does not match.
a = 800000000000ABC000000000000 ->  does not match.
a = ABC ->  does not match.
a = 900000000000000000000000 ->  matches.
a = -800000000000000000000000 ->  matches.
a = -900000000000000000000000 ->  matches.

Explanation:

  1. [A-Za-z] is for alphabets
  2. \\s+ is for space(s)
  3. [-]? is for optional -
  4. \\d+ is for digits

答案2

得分: 0

根据之前的回答和atmin的评论(我还无法在任何地方发表评论),你还可以通过以下方式排除前导零:

String regex = "0|(\\-?[1-9]\\d*)";
String stringToTest = "......";
boolean result = stringToTest.matches(regex);
英文:

expanding on the previous answer and the comment from atmin (i can't post comments anywhere yet), you could exclude leading zeroes as well with:

String regex = "0|(\\-?[1-9]\\d*)";
String stringToTest = "..."
boolean result = stringToTest.matches(regex);

huangapple
  • 本文由 发表于 2020年4月10日 18:59:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61138909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定