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 =;
    String jobnumber =;
    String originalFileName =;

Test your regex with this site:

No comments:

Post a Comment