Understanding Java Split with Regex

im currently learning Regex on java, there’s things that i dont understand,

I have this String

String str = I Buy 5 Potato with 5000 Dollar

and i have this regex

String regex = " (\d+)";

and then i try to split it using the regex

 String[] strings = str.split(regex);

it gives me this result

[I Buy,  Potato with,  Dollar]

My Question is, how do i get the number ? I expect the result will be [5, 500] but it returns [I Buy, Potato with, Dollar]

Am i missing something ?

Answer

String split divides a string on a given regular expression. Your regular expression matches 1 or more numeric characters. This is the “split point” (delimiter) so your string is divided at these number points.

Think about string split in this example:

String str = "a:b:c:d";
String regex = ":";
String[] strings = str.split(regex);
System.out.println(Arrays.toString(strings));

Output

[a, b, c, d]

See how it removes all “:” from the string and uses that as the division for the array of Strings? Now replace the concept of “any number” ((\d+)) with the “:” and you can see what’s happening with your example.

What you’re really looking for then is a regular expression that breaks on anything that’s not a number.

Something like:

String str = "I Buy 5 Potato with 5000 Dollar";
String regex = "[^\d]+";
String[] strings = str.split(regex);
System.out.println(Arrays.toString(strings));

Output

[, 5, 5000]

From this point your question becomes similar to this question How to extract numbers from a string and get an array of ints?

Leave a Reply

Your email address will not be published. Required fields are marked *