4

In the following string,

['*.*string1*.*'],['*.*String2*.*'],['*.*String3*.*'],['*.*String4*.*],['*.*String5*.*]

how can get the content including the brackets which are in String 4 using Regex expression.

I am hopping to get back the complete ['*.*String4*.*]. The regex should find the nearest '[' and ']' from 'string 4'.

I am currently using '\\[(.*?)string 4(.*)\\]' but its matching the first '[' and the last ']', meaning the entire length of the string is getting matched. I am sure it must be easy but i am not getting it, help me pls!

  • 2
    Would it just not be simpler to use a string tokenizer I.e. split on the comma character – techtrekker Mar 16 '13 at 19:17
  • @techtrekker, yes that was one of the suggestion that one of our team mates gave too. But there may lots of arrays like that, maybe 500-600 in a single string. I felt that splitting the string will not be good design and was reading about the regex search and replace function hence got the regex idea. I was under the impression that Regex is faster and simpler than string methods, do you agree or is it better to go for the good old string methods. – Anil Shivaraj Mar 16 '13 at 19:25
8

Thanks for showing us what you attempted - you were pretty close to the mark Anil :-)

You can achieve what you want by making the match non-greedy, or making it avoid square brackets.

public class AnilTest {

  static testmethod void testRegex() {
    String search = Pattern.quote('String4');
    String subject = '[\'*.*string3*.*\'],[\'*.*String4*.*\'],[\'*.*String5*.*\']';
    Pattern brackets = Pattern.compile(
      '\\['       //literal left bracket
      + '[^\\[]*' //zero or more of anything other than a left bracket
      + search.   //the search term, escaped or 'quoted' for regex
      + '[^\\]]*' //zero or more of anything other than a right bracket
      + '\\]'     //literal right bracket
    );
    Matcher matcher = brackets.matcher(subject);
    System.assert(matcher.find());
    System.assertEquals('[\'*.*String4*.*\']', matcher.group(0));
  }

}
  • awesome...thanks @user320 that works ...also one more small request can you also explain the matcher, i am still learning regex but couldnt reason out the \] which is there for the second time. – Anil Shivaraj Mar 16 '13 at 23:14
  • 3
    no wuckers @Anil - in regular expressions, brackets [] are used to describe a character range, which is why you'll see [a-z] etc. The caret ^ negates the range, to describe "anything but" the character range. Since bracket characters have special meaning, we have to escape them with a backslash \. And literal backslashes need to be escaped themselves, hence [^\\[] matching any characters not being in the range of a left bracket. – bigassforce Mar 16 '13 at 23:49
  • 1
    Check out the Java matcher methods: docs.oracle.com/javase/7/docs/api/java/util/regex/… @Anil – bigassforce Mar 16 '13 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.