I'm struggling with a "simple" RegEx problem. I wan't to check a sting if the pattern "BN123456" or "bn123456" is included and if so, extract it. The pattern could be in the beginning, middle or end of the string.
So I setup this regex and this test code
Pattern MyPattern = Pattern.compile('[Bb][Nn][0-9]{6}');
Matcher MyMatcher = MyPattern.matcher(' BN123456 bn123456');
System.debug('Match?' + MyMatcher.matches());
while(MyMatcher.find()){
System.debug('check ' + MyMatcher.group(0));
}
While the RegEx is working on RegEx101 it isn't in the Pattern/Matcher setting. The Problem is, that it finds 'BN123456' only with a space infront ' BN123456'. Any idea why this is the case?