正则表达式在字符串中查找整数
我想在 Java 中使用正则表达式。
我想要做的是找到字符串中的第一个整数。
示例:
String = "the 14 dogs ate 12 bones"
会返回 14。
String = "djakld;asjl14ajdka;sdj"
也会返回 14。
这是我到目前为止所得到的。
Pattern intsOnly = Pattern.compile("\\d*");
Matcher makeMatch = intsOnly.matcher("dadsad14 dssaf jfdkasl;fj");
makeMatch.find();
String inputInt = makeMatch.group();
System.out.println(inputInt);
我究竟做错了什么?
I'd like to use regex with Java.
What I want to do is find the first integer in a string.
Example:
String = "the 14 dogs ate 12 bones"
Would return 14.
String = "djakld;asjl14ajdka;sdj"
Would also return 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您要求输入 0 个或更多数字。 您需要询问 1 个或多个:
You're asking for 0 or more digits. You need to ask for 1 or more:
看起来其他解决方案无法处理
+/-
以及2e3
等情况,而java.lang.Integer.parseInt(String)
支持,所以我会解决这个问题。 我对正则表达式有点缺乏经验,所以我可能犯了一些错误,使用了Java的正则表达式解析器不支持的东西,或者使它变得过于复杂,但这些语句似乎在 Kiki 0.5.6。所有正则表达式都以供读取的非转义格式和可在 Java 中用作字符串文字的转义格式提供。
从中获取字节、短整型、整数或长整型字符串:
...以及奖励积分...
从字符串中获取双精度或浮点数:
It looks like the other solutions failed to handle
+/-
and cases like2e3
, whichjava.lang.Integer.parseInt(String)
supports, so I'll take my go at the problem. I'm somewhat inexperienced at regex, so I may have made a few mistakes, used something that Java's regex parser doesn't support, or made it overly complicated, but the statements seemed to work in Kiki 0.5.6.All regular expressions are provided in both an unescaped format for reading, and an escaped format that you can use as a string literal in Java.
To get a byte, short, int, or long from a string:
...and for bonus points...
To get a double or float from a string:
使用其中之一:
或
Use one of them:
or
这是我用泛型为 C# 制作的一个方便的工具。 它将根据您的正则表达式进行匹配并返回您需要的类型:
然后,如果您只想获取数字并将它们返回到 string[] 数组中:
希望这对某人有用......
Heres a handy one I made for C# with generics. It will match based on your regular expression and return the types you need:
then if you wanted to grab only the numbers and return them in an string[] array:
Hopefully this is useful to someone...
除了 PiPeep 所说的之外,如果您尝试匹配表达式中的整数,则
1 + 2 - 3
将仅匹配1
、2 和
3
,而不是1
、+ 2
和- 3
,您实际上需要使用Lookbehind 语句,您想要的部分实际上将由Matcher.group(2)
返回,而不仅仅是Matcher.group()
。另外,对于像
someNumber - 3
这样的东西,其中someNumber
是一个变量名或类似的东西,你可以使用虽然当然,如果你解析像这样的字符串,这将不起作用
blahblah 的净变化为 +4
In addition to what PiPeep said, if you are trying to match integers within an expression, so that
1 + 2 - 3
will only match1
,2
, and3
, rather than1
,+ 2
and- 3
, you actually need to use a lookbehind statement, and the part you want will actually be returned byMatcher.group(2)
rather than justMatcher.group()
.Also, for things like
someNumber - 3
, wheresomeNumber
is a variable name or something like that, you can useAlthough of course that wont work if you are parsing a string like
The net change to blahblah was +4
java 规范实际上提供了一个用于解析双精度数的正则表达式怪物。
然而,这被认为是不好的做法,只是尝试用预期的类型进行解析并捕获错误,往往更具可读性。
the java spec actually gives this monster of a regex for parsing doubles.
however it is considered bad practice, just trying to parse with the intended type, and catching the error, tends to be slightly more readable.