0

How split a [0] like words from string using regex pattern.0 can replace any integer number. I used regex pattern,

private static final String REGEX = "[\\d]";

But it returns string with [.

Spliting Code

Pattern p=Pattern.compile(REGEX);
String items[] = p.split(lure_value_save[0]);
5
  • What is your desired output? Commented Jul 12, 2013 at 13:56
  • @JavaDevil i am using Pattern p=Pattern.compile(REGEX); String items[]=p.split(lure_value_save[0]); Commented Jul 12, 2013 at 13:58
  • Do a match, not a split Commented Jul 12, 2013 at 13:58
  • Why not splitting by comma? List<String> list = Arrays.asList(str.split(",")); Commented Jul 12, 2013 at 13:59
  • @RogerRapid Sorry.here is little problem.String either contain [0] or [1].etc Commented Jul 12, 2013 at 14:32

2 Answers 2

1

You have to escape the brackets:

String REGEX = "\\[\\d+\\]";
Sign up to request clarification or add additional context in comments.

Comments

1

Java doesn't offer an elegant solution to extract the numbers. This is the way to go:

Pattern p = Pattern.compile(REGEX);

String test = "[0],[1],[2]";
Matcher m = p.matcher(test);

List<String> matches = new ArrayList<String>();     
while (m.find()) {
    matches.add(m.group());
}

Comments

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.