+ Start a Discussion

Loop through regex matches

How would one go about looping through all of the matches in a Matcher object? Most of the apex documentation says things like "from the previous match" but I can't even tell how to move between the matches (like previous and next match). Can anyone point me in the right direction, or better yet, post a sample?



Best Answer chosen by Admin (Salesforce Developers) 

Figured this out:



Pattern p = Pattern.compile('regex'); Matcher mo = p.matcher('string'); while (mo.find()) { System.debug(mo.group()); }