1

How can I parse such data with Java regex?

data.param[0] 
0 - is any non negavite digit

Here is my try -

String result = Pattern.compile("(data.param[)(\\d+)(])").matcher("data.param[13123]").group();
2
  • Possible duplicate? parse String with Regex Commented Sep 16, 2014 at 16:15
  • What is the output you are expecting? The digit in between the square brackets? The param[digit] idiom? Note that your . should be double-escaped in the Pattern. Commented Sep 16, 2014 at 16:15

1 Answer 1

5

You can use this regex:

Pattern p = Pattern.compile("data\\.param\\[(\\d+)\\]");
Matcher m = p.matcher("data.param[13123]");

if (m.find())
    System.out.println(m.group(1));
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. It's fine solution. Thanks. Will accept it in a 9 mins.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.