Java 正则表达式在字符串中查找整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/372148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:58:04  来源:igfitidea点击:

Regex to find an integer within a string

javaregexinteger

提问by mscccc

I'd like to use regex with Java.

我想在 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.

将返回 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?

我究竟做错了什么?

采纳答案by mscccc

You're asking for 0 or more digits. You need to ask for 1 or more:

您要求输入 0 位或更多位数字。您需要要求 1 个或多个:

"\d+"

回答by Chris Smith

Heres a handy one I made for C# with generics. It will match based on your regular expression and return the types you need:

这是我用泛型为 C# 制作的一个方便的。它将根据您的正则表达式进行匹配并返回您需要的类型:

public T[] GetMatches<T>(string Input, string MatchPattern) where T : IConvertible
    {
        List<T> MatchedValues = new List<T>();
        Regex MatchInt = new Regex(MatchPattern);

        MatchCollection Matches = MatchInt.Matches(Input);
        foreach (Match m in Matches)
            MatchedValues.Add((T)Convert.ChangeType(m.Value, typeof(T)));

        return MatchedValues.ToArray<T>();
    }

then if you wanted to grab only the numbers and return them in an string[] array:

那么如果您只想获取数字并将它们返回到 string[] 数组中:

string Test = "22$data44abc";
string[] Matches = this.GetMatches<string>(Test, "\d+");

Hopefully this is useful to someone...

希望这对某人有用...

回答by bgw

It looks like the other solutions failed to handle +/-and cases like 2e3, which java.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.

看起来其他解决方案无法处理,并且支持 , 之+/-类的情况,所以我会解决这个问题。我对 regex 有点缺乏经验,所以我可能犯了一些错误,使用了 Java 的 regex 解析器不支持的东西,或者使它过于复杂,但这些语句似乎在Kiki 0.5.6 中工作。2e3java.lang.Integer.parseInt(String)

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.

所有正则表达式都以用于读取的非转义格式和可在 Java 中用作字符串文字的转义格式提供。

To get a byte, short, int, or long from a string:

从字符串中获取字节、短整型、整型或长整型:

unescaped: ([\+-]?\d+)([eE][\+-]?\d+)?
  escaped: ([\+-]?\d+)([eE][\+-]?\d+)?

...and for bonus points...

......还有奖励积分......

To get a double or float from a string:

要从字符串中获取双精度或浮点数:

unescaped: ([\+-]?\d(\.\d*)?|\.\d+)([eE][\+-]?(\d(\.\d*)?|\.\d+))?
  escaped: ([\+-]?\d(\.\d*)?|\.\d+)([eE][\+-]?(\d(\.\d*)?|\.\d+))?

回答by AJMansfield

In addition to what PiPeep said, if you are trying to match integers within an expression, so that 1 + 2 - 3will only match 1, 2, and 3, rather than 1, + 2and - 3, you actually need to use a lookbehind statement, and the part you want will actually be returned by Matcher.group(2)rather than just Matcher.group().

除了 PiPeep 所说的,如果您尝试匹配表达式中的整数,那么1 + 2 - 3只会匹配1, 2, and 3,而不是1, + 2and - 3,您实际上需要使用lookbehind语句,并且您想要的部分实际上将被返回byMatcher.group(2)而不仅仅是Matcher.group()

unescaped: ([0-9])?((?(1)(?:[\+-]?\d+)|)(?:[eE][\+-]?\d+)?)
  escaped: ([0-9])?((?(1)(?:[\+-]?\d+)|)(?:[eE][\+-]?\d+)?)

Also, for things like someNumber - 3, where someNumberis a variable name or something like that, you can use

另外,对于喜欢的东西someNumber - 3,在这里someNumber是一个变量名称或类似的东西,你可以用

unescaped: (\w)?((?(1)(?:[\+-]?\d+)|)(?:[eE][\+-]?\d+)?)
  escaped: (\w)?((?(1)(?:[\+-]?\d+)|)(?:[eE][\+-]?\d+)?)

Although of course that wont work if you are parsing a string like The net change to blahblah was +4

虽然如果你解析一个字符串,当然这不会工作 The net change to blahblah was +4

回答by user3034617

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.

java规范实际上给了这个解析双打正则表达式的怪物。然而,它被认为是不好的做法,只是尝试使用预期的类型进行解析并捕获错误,往往会稍微更具可读性。

DOUBLE_PATTERN = Pattern
        .compile("[\x00-\x20]*[+-]?(NaN|Infinity|((((\p{Digit}+)(\.)?((\p{Digit}+)?)"
                + "([eE][+-]?(\p{Digit}+))?)|(\.((\p{Digit}+))([eE][+-]?(\p{Digit}+))?)|"
                + "(((0[xX](\p{XDigit}+)(\.)?)|(0[xX](\p{XDigit}+)?(\.)(\p{XDigit}+)))"
                + "[pP][+-]?(\p{Digit}+)))[fFdD]?))[\x00-\x20]*");

回答by Sanjaya Deshapriya

Use one of them:

使用其中之一:

Pattern intsOnly = Pattern.compile("[0-9]+");

or

或者

Pattern intsOnly = Pattern.compile("\d+");