1

I've got this regexp that I found in this thread that does not exactly working for me, I get System.StringException: No match found.

Basically, I want to get everything between [ ].

Pattern brackets = Pattern.compile('\\[');
string asf='[ { asdf } ]';
    Matcher matcher = brackets.matcher(asf);                
        system.debug(matcher.group(0));

Edit: I need to match curly braces too

1 Answer 1

2

There is a very good blog post here for Regex usage:

Using Regex in APEX

It suits your need. I just modified the Regex to this: \\[\\{([^}]*)\\}\\] and was able to read content inside [{ }].

EDIT:

Sample Code for the requirement:

string subject = 'this is a test [ {12312-D} ] email subject [dfasdfa]';
Pattern p = Pattern.compile('\\[ \\{([^}]*)\\} \\]');
Matcher m = p.matcher(subject);
if (m.find()) 
{
   system.debug('\n\n@@ : '+m.group(1)+'\n\n'); 
}
2
  • Does not match if there is whitespace between [ and {
    – dzh
    Feb 14, 2015 at 16:23
  • :-) .. It was pretty much straight forward. I've updated my answer with a sample code extracted and modified for your requirement from the same blog post.
    – VarunC
    Feb 15, 2015 at 18:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .