Pages

Tuesday, August 9, 2011

Java -=- Regex find example

Here's a simple example that shows how to find the value of different groups in a string with a regular expression.

Pattern pattern = Pattern.compile("^([a-zA-Z0-9]*)_(\\d*)?_?(.*)-\\d*_.*$");
Matcher m = pattern.matcher(resourceFile.getName());

if (m.find()) {
    String destinationNickname = m.group(1);
    String jobnumber = m.group(2);
    String originalFileName = m.group(3);
} 

Test your regex with this site: http://www.regexplanet.com/simple/index.html

No comments:

Post a Comment