我想要一个if条件,只有当一个字符串包含多于四位数时才为真,如果字符串中包含少于四位数,则为false,我试过像/ \ d {4} /这样的regex,需要帮助
答案 0 :(得分:1)
以下模式将匹配包含至少4位数字的字符串:
awk '/pattern/{print field where pattern found}' #Howto
答案 1 :(得分:1)
public static void main(String[] args) {
String toCheck1 = "assg3asgasgas123aassag3";
String toCheck2 = "aasdasfasfs";
System.out.println(String.format("more then 4 number in \"%s\" - %s", toCheck1, moreThen4NumbersInString(toCheck1)));
System.out.println(String.format("more then 4 number in \"%s\" - %s", toCheck2, moreThen4NumbersInString(toCheck2)));
}
private static boolean moreThen4NumbersInString(String string) {
int numberOfNumbers = 0;
for (int i = 0; i < string.length(); i++) {
if (Character.isDigit(string.charAt(i))) {
numberOfNumbers++;
if (numberOfNumbers > 4) {
return true;
}
}
}
return false;
}
输出:
&#34; assg3asgasgas123aassag3&#34; - 真的超过4 &#34; aasdasfasfs&#34; - 假
答案 2 :(得分:0)
将$ awk 'NR==1||$2>1' test.in
A 1 A1g DELL
A 7 Zfd Nog
A 5 Jgf KIT
转换为string
。 char[]
- 遍历数组的所有元素并计算for
中的数字位数。就这么简单
答案 3 :(得分:0)
答案 4 :(得分:0)
在这里你可以找到一个样本:
package com.company.misc;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexSample {
public static void main(String[] args) {
String regex = "(.*?\\d){4,}";
//(.*?\d){4, } do not use this, compilation error
String input = "test2531";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
boolean isMatched = matcher.matches();
System.out.println(isMatched);
}
}
希望我已经举例说明了你的用例。