Skip to main content
Tweeted twitter.com/StackSalesforce/status/761337732589359104
added 40 characters in body
Source Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431

I have a regular expression that I want to use to pull ID's out of a JSON string. The expression itself appears to work fine based on the data I've given it, so that's good. Unfortunately

^(?:.*)(?:{"id":)(\d+)(?:.*)$

Unfortunately, it involves more than one non-capturing group ("(?:)"), and that seems to break Apex with the dreaded "Invalid regex at index 9." Is there actually a 1 non-capture group limit in Apex's regex flavor or is there something else wrong with this? I wasn't able to find any documentation on such a limit, so i'm inclined to think that there is some other error in my regex, but I can't find it!

Just for completeness, here's an example that is failing on my sandbox.

pattern myPattern = pattern.compile('^(?:.*)(?:{"id":)$'); 
matcher myMatcher = myPattern.matcher('{"user":{"id":11111,"userName":');
System.assert(myMatcher.matches() && myMatcher.hitEnd());

if(myMatcher.matches())
{
    system.debug(myMatcher.group(1));
}

*** bolded for easier skimming**

I have a regular expression that I want to use to pull ID's out of a JSON string. The expression itself appears to work fine based on the data I've given it, so that's good. Unfortunately, it involves more than one non-capturing group ("(?:)"), and that seems to break Apex with the dreaded "Invalid regex at index 9." Is there actually a 1 non-capture group limit in Apex's regex flavor or is there something else wrong with this? I wasn't able to find any documentation on such a limit, so i'm inclined to think that there is some other error in my regex, but I can't find it!

Just for completeness, here's an example that is failing on my sandbox.

pattern myPattern = pattern.compile('^(?:.*)(?:{"id":)$'); 
matcher myMatcher = myPattern.matcher('{"user":{"id":11111,"userName":');
System.assert(myMatcher.matches() && myMatcher.hitEnd());

if(myMatcher.matches())
{
    system.debug(myMatcher.group(1));
}

*** bolded for easier skimming**

I have a regular expression that I want to use to pull ID's out of a JSON string. The expression itself appears to work fine based on the data I've given it, so that's good.

^(?:.*)(?:{"id":)(\d+)(?:.*)$

Unfortunately, it involves more than one non-capturing group ("(?:)"), and that seems to break Apex with the dreaded "Invalid regex at index 9." Is there actually a 1 non-capture group limit in Apex's regex flavor or is there something else wrong with this? I wasn't able to find any documentation on such a limit, so i'm inclined to think that there is some other error in my regex, but I can't find it!

Just for completeness, here's an example that is failing on my sandbox.

pattern myPattern = pattern.compile('^(?:.*)(?:{"id":)$'); 
matcher myMatcher = myPattern.matcher('{"user":{"id":11111,"userName":');
System.assert(myMatcher.matches() && myMatcher.hitEnd());

if(myMatcher.matches())
{
    system.debug(myMatcher.group(1));
}

*** bolded for easier skimming**

Source Link
Grisk
  • 256
  • 1
  • 9

Apex regex and non-capturing groups

I have a regular expression that I want to use to pull ID's out of a JSON string. The expression itself appears to work fine based on the data I've given it, so that's good. Unfortunately, it involves more than one non-capturing group ("(?:)"), and that seems to break Apex with the dreaded "Invalid regex at index 9." Is there actually a 1 non-capture group limit in Apex's regex flavor or is there something else wrong with this? I wasn't able to find any documentation on such a limit, so i'm inclined to think that there is some other error in my regex, but I can't find it!

Just for completeness, here's an example that is failing on my sandbox.

pattern myPattern = pattern.compile('^(?:.*)(?:{"id":)$'); 
matcher myMatcher = myPattern.matcher('{"user":{"id":11111,"userName":');
System.assert(myMatcher.matches() && myMatcher.hitEnd());

if(myMatcher.matches())
{
    system.debug(myMatcher.group(1));
}

*** bolded for easier skimming**