mscccc
mscccc

Reputation: 2275

Regex to find an integer within a string

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?

Upvotes: 24

Views: 82611

Answers (6)

Sanjaya Deshapriya
Sanjaya Deshapriya

Reputation: 19

Use one of them:

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

or

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

Upvotes: 1

user3034617
user3034617

Reputation: 21

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

Upvotes: 0

AJMansfield
AJMansfield

Reputation: 4175

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

Upvotes: 0

user3458
user3458

Reputation:

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

"\\d+"

Upvotes: 54

bgw
bgw

Reputation: 2056

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+))?

Upvotes: 3

Chris Smith
Chris Smith

Reputation: 688

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

Upvotes: 0

Related Questions