function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Markus Koch 7Markus Koch 7 

Regex exception even if matcher.find() is true

Hey guys,

I have an error that I don't get. Here is my code:
pattern contractNumberPattern = pattern.compile('[0-9]{7}'); 
Matcher contractNumberMatcher = contractNumberPattern.matcher('Test1234567Test');   
String contractNumber;
system.debug(contractNumberMatcher.find());
system.debug(contractNumberMatcher.matches());
system.debug(contractNumberMatcher.groupCount());
contractNumber = contractNumberMatcher.group(1);
system.debug(contractNumber);
It should find a 7 digit number inside a string and return this, But even though find() returns true the groupcount is 0 and I get an error when accessing group(1).
What am I doing wrong here? Thanks in advance!