10

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.

3
  • 1
    substring first. then split Commented Mar 4, 2014 at 11:42
  • @MustafaGenç, what if each integer is wrapped with []? Commented Mar 4, 2014 at 11:43
  • Try yourString = yourString.replace ( /[^\d.]/g, '' ); Commented Mar 4, 2014 at 11:44

8 Answers 8

15

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

String filtered = str.replaceAll("[^0-9,]","");
String[] numbers = filtered.split(",");
Sign up to request clarification or add additional context in comments.

3 Comments

Oh, really, how could I miss replaceAll. Thanks, that is what I tried to do.
This is possible, but does not allow changing the delimiter easily, with a source like [1][2],[3] for example you will run into problems
For all we know the desired output for [0 1,2] might as well be 01 2. Everything not mentioned by OP is a speculation.
2

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.

Comments

1

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

Use replace method to do this.

Comments

1

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

Comments

1

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

Comments

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

this will give you the expected string.

2 Comments

If each integer is wrapped with [], this won't work.
@Dragon it that case is possible then you should have mentioned it in your question.
0

Try doing this to the code:

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

Comments

0

replace the brackets [ ]and then split

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

1 Comment

instead of "\\[", it's better to use raw strings: r"\["

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.