user2251725
user2251725

Reputation: 406

How split a string using regex pattern

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

Upvotes: 0

Views: 143

Answers (2)

jlordo
jlordo

Reputation: 37813

You have to escape the brackets:

String REGEX = "\\[\\d+\\]";

Upvotes: 1

winne2
winne2

Reputation: 2250

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());
}

Upvotes: 1

Related Questions