英文:
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 -> 匹配。
解释:
[A-Za-z]
表示字母\\s+
表示一个或多个空格[-]?
表示可选的-
符号\\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:
[A-Za-z]
is for alphabets\\s+
is for space(s)[-]?
is for optional-
\\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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论