This RegExp is working properly here: https://regexr.com/4j143 However, when I try to use it in Apex it is not working: String regExp = '/\\s{1,}(ORDER BY)\\s{1,}(ID)(\\s{1,}(ASC)){0,1}(\\s{1,}(NULLS)\\s{1,}(LAST)){0,1}\\s{0,},/gi'; String input = 'SELECT * FROM ... ORDER BY ID, INDUSTRY ASC'; System.debug(Pattern.matches(regExp, input)); Can somebody spot my mistake?