Regex to find an integer within a string

2020-02-26 04:28发布

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?

6条回答
看我几分像从前
2楼-- · 2020-02-26 04:40

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+))?
查看更多
唯我独甜
3楼-- · 2020-02-26 04:48

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...

查看更多
狗以群分
4楼-- · 2020-02-26 04:52

Use one of them:

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

or

Pattern intsOnly = Pattern.compile("\\d+");
查看更多
劫难
5楼-- · 2020-02-26 04:57

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

"\\d+"
查看更多
我欲成王,谁敢阻挡
6楼-- · 2020-02-26 04:59

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

查看更多
▲ chillily
7楼-- · 2020-02-26 05:00

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]*");
查看更多
登录 后发表回答