检查字符串是否包含Java数字
我正在编写一个程序,其中用户输入以下格式的字符串:检查字符串是否包含Java数字,java,regex,string,Java,Regex,String,我正在编写一个程序,其中用户输入以下格式的字符串: "What is the square of 10?" 我需要检查字符串中是否有数字 然后只提取数字 如果使用.contains(\\d+”)或.contains([0-9]+”),则无论输入是什么,程序都无法在字符串中找到数字,但是。匹配(\\d+”)仅在只有数字时才起作用 我可以使用什么作为查找和提取的解决方案?尝试以下模式: .matches("[a-zA-Z ]*\\d+.*") 试试这个 str.matches(".*\\d.*"
"What is the square of 10?"
.contains(\\d+”)
或.contains([0-9]+”)
,则无论输入是什么,程序都无法在字符串中找到数字,但是。匹配(\\d+”)
仅在只有数字时才起作用我可以使用什么作为查找和提取的解决方案?尝试以下模式:
.matches("[a-zA-Z ]*\\d+.*")
试试这个
str.matches(".*\\d.*");
你可以试试这个
String text = "ddd123.0114cc";
String numOnly = text.replaceAll("\\p{Alpha}","");
try {
double numVal = Double.valueOf(numOnly);
System.out.println(text +" contains numbers");
} catch (NumberFormatException e){
System.out.println(text+" not contains numbers");
}
因为您不仅要查找数字,还要提取它,所以应该编写一个小函数来完成这项工作。一个字母接一个字母,直到你找到一个数字。啊,刚刚在stackoverflow上找到了您需要的代码:。看看公认的答案。我认为它比正则表达式快
public final boolean containsDigit(String s) {
boolean containsDigit = false;
if (s != null && !s.isEmpty()) {
for (char c : s.toCharArray()) {
if (containsDigit = Character.isDigit(c)) {
break;
}
}
}
return containsDigit;
}
如果要从输入字符串中提取第一个数字,可以执行以下操作-
public static String extractNumber(final String str) {
if(str == null || str.isEmpty()) return "";
StringBuilder sb = new StringBuilder();
boolean found = false;
for(char c : str.toCharArray()){
if(Character.isDigit(c)){
sb.append(c);
found = true;
} else if(found){
// If we already found a digit before and this char is not a digit, stop looping
break;
}
}
return sb.toString();
}
示例:
对于输入“123abc”,上述方法将返回123
对于“abc1000def”,为1000
对于“555abc45”,555
对于“abc”,将返回一个空字符串
我使用的解决方案如下所示:
Pattern numberPat = Pattern.compile("\\d+");
Matcher matcher1 = numberPat.matcher(line);
Pattern stringPat = Pattern.compile("What is the square of", Pattern.CASE_INSENSITIVE);
Matcher matcher2 = stringPat.matcher(line);
if (matcher1.find() && matcher2.find())
{
int number = Integer.parseInt(matcher1.group());
pw.println(number + " squared = " + (number * number));
}
(x >= 97 && x <= 122) || (x >= 65 && x <= 90) // 97 == 'a' and 65 = 'A'
我肯定这不是一个完美的解决方案,但它适合我的需要。谢谢大家的帮助。:) 下面的代码足以“检查Java中的字符串是否包含数字”
s=s.replaceAll(“[*a-zA-Z]”,“”)
替换所有字母表
s=s.replaceAll(“[*0-9]”,“”)
替换所有数字
如果您执行上述两个替换,您将获得所有特殊字符字符串
如果只想从字符串s=s.replaceAll(“[^0-9]”,“”)中提取整数
如果只想从字符串s=s.replaceAll(“[^a-zA-Z]”,“”)中提取字母表
快乐编码:)公共字符串hasNums(字符串str){
char[]nums={'0','1','2','3','4','5','6','7','8','9'};
char[]toChar=新字符[str.length()];
对于(int i=0;i
.matches(.*\\d+.*)
仅适用于数字,而不适用于其他符号,如/
或*
等。ASCII位于UNICODE的开头,因此您可以执行以下操作:
Pattern numberPat = Pattern.compile("\\d+");
Matcher matcher1 = numberPat.matcher(line);
Pattern stringPat = Pattern.compile("What is the square of", Pattern.CASE_INSENSITIVE);
Matcher matcher2 = stringPat.matcher(line);
if (matcher1.find() && matcher2.find())
{
int number = Integer.parseInt(matcher1.group());
pw.println(number + " squared = " + (number * number));
}
(x >= 97 && x <= 122) || (x >= 65 && x <= 90) // 97 == 'a' and 65 = 'A'
(x>=97&&x=65&&x我找不到一个正确的模式。
请按照下面的指南寻找小而甜的解决方案
String regex = "(.)*(\\d)(.)*";
Pattern pattern = Pattern.compile(regex);
String msg = "What is the square of 10?";
boolean containsNumber = pattern.matcher(msg).matches();
下面的代码片段将告诉您字符串是否包含数字
str.matches(".*\\d.*")
or
str.matches(.*[0-9].*)
例如
String str = "abhinav123";
str.matches(".*\\d.*") or str.matches(.*[0-9].*) will return true
str = "abhinav";
str.matches(".*\\d.*") or str.matches(.*[0-9].*) will return false
当我被重定向到这里,寻找一种在Kotlin
语言中查找字符串中数字的方法时,我将把我的发现留在这里,让其他想要特定于Kotlin的解决方案的人知道
查找字符串是否包含数字:
val hasDigits = sampleString.any { it.isDigit() }
查找字符串是否仅包含数字:
val hasOnlyDigits = sampleString.all { it.isDigit() }
从字符串中提取数字:
val onlyNumberString = sampleString.filter { it.isDigit() }
但是,如果用户礼貌地说“请”
,这将失败。此外,在OP的示例中,还有一个“?”要解释。您确定已修复它吗?此表达式不适用于类似于“10”这样的答案用于示例。如果要提取第一个数字,而不仅仅是输入字符串中的数字,请参阅我的答案。很抱歉,我没有看到提取。如果要提取字符串中的数字,可以轻松编辑此代码。双反斜杠的作用是什么?解释一下:.*表示从0到无限出现的任何字符,而不是\\d+(我认为双反斜杠只是为了避开第二个反斜杠)而\d+意味着一个从1倍到无穷大的数字。这不是外星魔法,它是神秘的BS,从没有人能做这件事的时代开始,而是发明它的工程师,因为它基本上是一个密码,只有创造者知道,直到他们共享它。修改,因为如果只有一个数字存在,解决方案就足够好了,不需要有1如果字符串包含反斜杠,那么我认为这将产生一个“非法转义字符”错误。这是第一行有小错误,字符串中缺少括号。模式p=Pattern.compile((([A-Z].[0-9]));此外,这不起作用…测试起作用,但123TEST不起作用。
val onlyNumberString = sampleString.filter { it.isDigit() }