Dragon
Dragon

Reputation: 2481

Java: string split by regexp to get only integers

I have a string: [1, 2, 3, 4]. I need to get only integers 1 2 3 4.

I tried the following splits:

str.split(",");
str.split("\\D\\s");

Both splits return four elements: [1 2 3 4], but I don't need these brackets [ ]. What is wrong with split regexp?

updated

I had to mention that the case when each number is wrapped with [ ] can occur.

Upvotes: 10

Views: 13169

Answers (8)

slim
slim

Reputation: 41223

Your code is doing exactly what you tell it: split the string using "," as a delimiter:

"[1, 2, 3]" => "[1" , " 2", " 3]"

You could massage the string into shape before using split() - to remove the spaces and the square brackets. However a more direct, regex way to do it is to use a Matcher to scan through the input string:

    Pattern pattern = Pattern.compile("(\\d+)\\D+");
    ArrayList<String> list = new ArrayList<String>();
    Matcher matcher = pattern.matcher(input);
    while(matcher.find()) {
        list.add(matcher.group(1));
    }
    // if you really need an array
    String[] array = list.toArray(new String[0]);

This pattern (\d+)\D+ matches one or more digits, followed by one or more non-digits, and it captures the digits in a group, which you can access using matcher.group(1). It matches your input quite loosely; you could make it more specific (so that it would reject input in other forms) if you preferred.

Upvotes: 2

maxdev
maxdev

Reputation: 2566

The regex that you give to the split method is used to determine the index of each split.

The cleanest solution to handle your case would be using Pattern.compile to create a pattern like [0-9]+ and then use the matcher to find each match in your source string.

Code example:

Pattern pattern = Pattern.compile("[0-9]+");
Matcher matcher = pattern.matcher(yourString);

while(matcher.find()) {
    System.out.println(matcher.group());
}

This prints out each consecutive number in yourString.


Appendix: if you use this piece of code often, create a static variable which compiles the Pattern already when loading the class, as pattern compilation is expensive. Like: private static final Pattern NUMBER_PATTERN = Pattern.compile("[0-9]+");

Upvotes: 1

Zeeshan
Zeeshan

Reputation: 12421

Try this:

String str = "[1, 2, 3, 4]";
String num = str.replaceAll( "[^\\d,]", "" );

you will get String 1,2,3,4

Then use String[] allNums = num.split(",");

EDIT: this will also work for the input String str = "[[1], [2], [3], [4]]";

Upvotes: 1

saravanakumar
saravanakumar

Reputation: 1777

Actually split function returns an array of string,That's why you getting this [].

Use replace method to do this.

Upvotes: 1

Nambi
Nambi

Reputation: 12042

replace the brackets [ ]and then split

str.replaceAll("\\[", "").replaceAll("\\]", "").split(",");

Upvotes: 0

Victor York
Victor York

Reputation: 1681

Try doing this to the code:

yourString = yourString.replace ( /[^\d.]/g, '' );

Upvotes: 0

Gundamaiah
Gundamaiah

Reputation: 786

String s=s.substring(1,s.length()-1);
String s1[]=s.split(",");

this will give you the expected string.

Upvotes: 0

NeplatnyUdaj
NeplatnyUdaj

Reputation: 6242

You could try filtering out unwanted elements first and then split:

String filtered = str.replaceAll("[^0-9,]","");
String[] numbers = filtered.split(",");

Upvotes: 15

Related Questions