作者:wjw0000 | 来源:互联网 | 2023-05-18 06:57
I'd like to use regex with Java.
我想在Java中使用regex。
What I want to do is find the first integer in a string.
我要做的是找到字符串中的第一个整数。
Example:
例子:
String = "the 14 dogs ate 12 bones"
Would return 14.
将返回14。
String = "djakld;asjl14ajdka;sdj"
Would also return 14.
还会返回14。
This is what I have so far.
这是我目前所拥有的。
Pattern intsOnly= Pattern.compile("\\d*");
Matcher makeMatch = intsOnly.matcher("dadsad14 dssaf jfdkasl;fj");
makeMatch.find();
String inputInt = makeMatch.group();
System.out.println(inputInt);
What am I doing wrong?
我做错了什么?
5 个解决方案