如何在java中为匹配String和BigInteger编写正则表达式?
我试图使用这个网站。但我没有成功 我的Java正则表达式:如何在java中为匹配String和BigInteger编写正则表达式?,java,Java,我试图使用这个网站。但我没有成功 我的Java正则表达式: (.*)\\w+=[0-9][0-9]*$(.*) 根据以下内容进行测试的条目: a = 800000000000000000000000 我的代码需要为数据提供true。匹配((.*)\\w+=[0-9][0-9]*$(.*))的a=80000000000000000000000按如下操作: public class Main { public static void main(String[] args) {
(.*)\\w+=[0-9][0-9]*$(.*)
根据以下内容进行测试的条目:
a = 800000000000000000000000
我的代码需要为数据提供true
。匹配((.*)\\w+=[0-9][0-9]*$(.*))
的a=80000000000000000000000
按如下操作:
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."));
}
}
}
输出:
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.
说明:
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.
[A-Za-z]
用于字母表\\s+
表示空格[-]?
是可选的-
\\d+
表示数字在前面的答案和atmin的评论(我还不能在任何地方发表评论)的基础上展开,您还可以排除前导零,包括:
String regex = "0|(\\-?[1-9]\\d*)";
String stringToTest = "..."
boolean result = stringToTest.matches(regex);
你到底想实现什么?@Mureinik我希望我的程序像data.matches一样工作((.*)\\w+=[0-9][0-9]*$(.*))==true@ArvindKumar Avinash不,它不适用于.matches()@plaza2009-请解释您的,而不仅仅是您尝试的解决方案。不,BigInteger是任意数字,800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000。负数呢?正则表达式应该是“[-]?\\d+”,它需要匹配“a=800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000?与8000000000000不同的是,可以有任何数字,这适用于任何整数(无论多大),但它不匹配前导0(需要1-9作为第一个数字),并且没有“-0”,只需取上一个答案并替换“=\\s+”之后的部分即可;同样,这只是一个补充,因为我不能在任何地方发表评论