Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从字符串中提取第一个找到的int_Java_Regex_Int - Fatal编程技术网

Java 从字符串中提取第一个找到的int

Java 从字符串中提取第一个找到的int,java,regex,int,Java,Regex,Int,所以我有字符串22test12344DC和1name23234343dc 我想要从字符串中提取第一个找到的完整int的最佳方法 这将从上面的例子中返回22和1。找到了第一个完整整数 我尝试了这种方法,但我不希望在第一个字符后有任何值 mystr.split("[a-z]")[0] 使用正则表达式和正确的模式可以实现以下目的: 然后打破while循环,因为您只需要第一个匹配 String myCodeString = "22test12344DC"; myCodeString = "1name

所以我有字符串22test12344DC和1name23234343dc

我想要从字符串中提取第一个找到的完整int的最佳方法

这将从上面的例子中返回22和1。找到了第一个完整整数

我尝试了这种方法,但我不希望在第一个字符后有任何值

mystr.split("[a-z]")[0]

使用正则表达式和正确的模式可以实现以下目的:

然后打破while循环,因为您只需要第一个匹配

String myCodeString = "22test12344DC";
myCodeString = "1name23234343dc";
Matcher matcher = Pattern.compile("\\d+|\\D+").matcher(myCodeString);

while (matcher.find()) {
    System.out.println(matcher.group());
    break;
}

使用正则表达式和正确的模式可以实现以下目的:

然后打破while循环,因为您只需要第一个匹配

String myCodeString = "22test12344DC";
myCodeString = "1name23234343dc";
Matcher matcher = Pattern.compile("\\d+|\\D+").matcher(myCodeString);

while (matcher.find()) {
    System.out.println(matcher.group());
    break;
}
试试这个

String s = "22test12344DC";
String firstInt = s.replaceFirst(".*?(\\d+).*", "$1");
System.out.println(firstInt);
结果:

22
试试这个

String s = "22test12344DC";
String firstInt = s.replaceFirst(".*?(\\d+).*", "$1");
System.out.println(firstInt);
结果:

22

考虑ReGEX来匹配字符串内的数字!我不希望在第一个字符之后有任何值。你想如何得到22,22Stest12124DC 22是第一个国际考虑ReGEX匹配字符串内的数字,而不是!我不希望在第一个字符之后有任何值。您希望如何获得22然后22测试12344DC 22是第一个int。