views:

2272

answers:

3

I'm new to using regex and I'd like to use it 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?

+15  A: 

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

"\\d+"
Arkadiy
That worked. Thank you!
mc6688
I generally avoid all things regex, but this is a really good use of it. Possibly the first time where it is more reliable and obvious than just coding it. +1 +1
Bill K
What's the difference between working and not working code? One stroke. Asterisk is three strokes intersecting each other, and plus is just two. There is a koan waiting to happen!
Arkadiy
A: 

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

Smitty
+1  A: 

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