1

I have to get index value from the following string:

Home.number[12].parent. 

I want to get back value of 12.

Here is what I tried:

//escape . / [ ]
 private static final String pattern = "Home\\.number\\[([0-9]*)\\]*";
 private static final Pattern addressPattern = Pattern.compile(pattern);

   private static int getIndex(String input, Pattern pattern){

        Matcher m = pattern.matcher(input);
        if (m.matches()){
        return Integer.valueOf(m.group(2));
        }
        return -1;
    }

    public static void main(String[] args){

    System.out.println(getIndex("Home.number[123].parent", addressPattern);
    System.out.println(getIndex("Home.number[456].child", addressPattern);   
}

I get back -1 for both, meaning no match is found.

Using the debugger, I found that m.matches() is returning false. I am unable to figure out why.

P.S: I also tried using Pattern.quote("Home.number[([0-9]*])*") and StringUtils.EscapeJava("Home.number[([0-9]*)]*"). Both are not returning any matching results.

3 Answers 3

3

Your Pattern should look something like

private static final String pattern = "Home\\.number\\[(\\d+)\\]\\..*";
private static final Pattern addressPattern = Pattern.compile(pattern);

And your matcher only has 1 group.

private static int getIndex(String input, Pattern pattern) {
    Matcher m = pattern.matcher(input);
    if (m.matches()) {
        return Integer.parseInt(m.group(1));
    }
    return -1;
}

And you need to close the second paren in your calls in main. Something like

public static void main(String[] args) {
    System.out.println(getIndex("Home.number[123].parent", addressPattern));
    System.out.println(getIndex("Home.number[456].child", addressPattern));
}

When I make those changes I get the expected

123
456
Sign up to request clarification or add additional context in comments.

4 Comments

is there no need to escape the dot in "Home.number"?
Escaping the dot does work - if you don't escape it then you can match HomeXnumber[123].cousin
what you suggested worked fine. I had two issues. one is espacing the dot and missing dot in the end for "].*". Thanks a ton
You want the last dot to be unescaped (since it needs to match the rest of the string) but the first one needs to be escaped (since you want to match a full stop not any character). Either Home\\.number\\[(\\d+)\\].* or Home\\.number\\[(\\d+)\\]\\..* will match.
1
  1. Change the pattern from: "Home\\.number\\[([0-9]*)\\]*" to "Home\\.number\\[([0-9]+)\\].*" (adding the dot before the last *)
  2. Change the group to #1: return Integer.valueOf(m.group(1));
  3. Add closing brackets to the System.out.println() calls.

Like this:

private static final String pattern = "Home\\.number\\[([0-9]*)\\].*";
private static final Pattern addressPattern = Pattern.compile(pattern);

private static int getIndex(String input, Pattern pattern){
  Matcher m = pattern.matcher(input);
  if (m.matches()){
    return Integer.valueOf(m.group(1));
  }
  return -1;
}

public static void main( String[] args ){
  System.out.println(getIndex("Home.number[123].parent", addressPattern));
  System.out.println(getIndex("Home.number[456].child", addressPattern));   
}

2 Comments

i am passing as method parameter. so it is fine
This code fails in case of Home.number[].parent. [0-9]* -> [0-9]+ or \\d+
1

If you remove everything except what between the square brackets, you can do it in one line:

private static int getIndex(String input) {
    return input.matches(".*\\[\\d+].*") ? -1 : Integer.parseInt(input.replaceAll(".*\\[|].*", ""));
}

1 Comment

@MTO hmmm. good point. I've replace changed the regex to leave only what's between the brackets, and I'm also checking there are only numbers between brackets.

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.