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

发布于 2024-07-10 05:00:24 字数 488 浏览 7 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

御弟哥哥 2024-07-17 05:00:24

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

"\\d+"

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

"\\d+"
为人所爱 2024-07-17 05:00:24

看起来其他解决方案无法处理 +/- 以及 2e3 等情况,而 java.lang.Integer.parseInt(String) 支持,所以我会解决这个问题。 我对正则表达式有点缺乏经验,所以我可能犯了一些错误,使用了Java的正则表达式解析器不支持的东西,或者使它变得过于复杂,但这些语句似乎在 Kiki 0.5.6

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

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

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

...以及奖励积分...

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

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

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.

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:

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+))?
半﹌身腐败 2024-07-17 05:00:24

使用其中之一:

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

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

Use one of them:

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

or

Pattern intsOnly = Pattern.compile("\\d+");
ㄖ落Θ余辉 2024-07-17 05:00:24

这是我用泛型为 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>();
    }

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

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

希望这对某人有用......

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

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 Test = "22$data44abc";
string[] Matches = this.GetMatches<string>(Test, "\\d+");

Hopefully this is useful to someone...

温柔戏命师 2024-07-17 05:00:24

除了 PiPeep 所说的之外,如果您尝试匹配表达式中的整数,则 1 + 2 - 3 将仅匹配 12 和 3,而不是 1+ 2- 3,您实际上需要使用Lookbehind 语句,您想要的部分实际上将由 Matcher.group(2) 返回,而不仅仅是 Matcher.group()

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

另外,对于像 someNumber - 3 这样的东西,其中 someNumber 是一个变量名或类似的东西,你可以使用

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

虽然当然,如果你解析像这样的字符串,这将不起作用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 match 1, 2, and 3, rather than 1, + 2 and - 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().

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

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

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

沫离伤花 2024-07-17 05:00:24

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]*");

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.

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]*");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文