3 March 2010

Java Regular Expression Searching for Expression Language Script within a String

I recently needed to search for an Expression Language (el) script within a String e.g. "hello my name is people[${loop.index}].name". I decided to use RegEx as it's the most efficient solution. The following code snippet searches for multiple occurrences of el script blocks within a String and add's them to a List:

String haystack = "hello my name is people[${loop.index}].name";

Pattern pattern = Pattern.compile("\\$\\{\\w+(\\.\\w+)*\\}");

Matcher matcher = pattern.matcher(haystack);

List matches = new ArrayList();


No comments:

Post a Comment