I have a string that could be of this kind, for example :

String text = 'test AZ123AZ testtesttest';

I need to retrive this part of the string :


So for now I have something like this in my apex code :

String regexTest ='[A-Z]{2}[0-9]{3}[A-Z]{2}';
Matcher matcher =Pattern.compile(regexTest ).matcher(text);
Boolean result = matcher.find();

The boolean result gives me true if the regex type is in a string so it's ok, but I need to retrive the value of the regex to put it into another string (only this part of the string text is important for me)

1 Answer 1


You can iterate over matching segments of your text using matcher.group():

while (matcher.find()) {

If you know you only have one match, or only want the first match, you can just call matcher.group() directly without using a loop.

You must log in to answer this question.

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