6

I've been trying to split Strings using RegEx with no success. The idea is to split a given music file metadata from its file name in a way so that:

"01. Kodaline - Autopilot.mp3"

.. would result in..

metadata[0] = "01"
metadata[1] = "Kodaline"
metadata[2] = "Autopilot"

This is the RegEx I've been trying to use in its original form:

^(.*)\.(.*)\-(.*)\.(mp3|flac)

From what I've read, I need to format the RegEx for String.split(String regex) to work. So here's my formatted RegEx:

^(.*)\\.(.*)\\-(.*)\\.(mp3|flac)

..and this is what my code looks like:

String filename = "01. Kodaline - Autopilot.mp3";
String regex = "^(.*)\\.(.*)\\-(.*)\\.(mp3|flac)";

String[] metadata = filename.split(regex);

But I'm not receiving the result I expected. Can you help me on this?

3 Answers 3

7

Your regex is fine for matching the input string. Your problem is that you used split(), which expects a regex with a totally different purpose. For split(), the regex you give it matches the delimiters (separators) that separate parts of the input; they don't match the entire input. Thus, in a different situation (not your situation), you could say

String[] parts = s.split("[\\- ]");

The regex matches one character that is either a dash or a space. So this will look for dashes and spaces in your string and return the parts separated by the dashes and spaces.

To use your regex to match the input string, you need something like this:

String filename = "01. Kodaline - Autopilot.mp3";
String regex = "^(.*)\\.(.*)\\-(.*)\\.(mp3|flac)";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(filename);

String[] metadata = new String[4];
if (matcher.find()) {
    metadata[0] = matcher.group(1); // in real life I'd use a loop
    metadata[1] = matcher.group(2);
    metadata[2] = matcher.group(3);
    metadata[3] = matcher.group(4);
    // the rest of your code
}

which sets metadata to the strings "01", " Kodaline ", " Autopilot", "mp3", which is close to what you want except maybe for extra spaces (which you can look for in your regex). Unfortunately, I don't think there's a built-in Matcher function that returns all the groups in one array.

(By the way, in your regex, you don't need the backslashes in front of -, but they're harmless, so I left them in. The - doesn't normally have a special meaning, so it doesn't need to be escaped. Inside square brackets, however, a hyphen is special, so you should use backslashes if you want to match a set of characters and a hyphen is one of those characters. That's why I used backslashes in my split example above.)

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I need and well explained. Thanks!
3

this worked for me

str.split("\\.\\s+|\\s+-\\s+|\\.(mp3|flac)");

2 Comments

Would you care to explain what each of those regex do? I'm having a hard time to comprehend what they mean.
The idea is the same as in your regex; .space(s) OR space(s)-space(s) OR .(mp3 OR flac)
1

Try something like:

String filename = "01. Kodaline - Autopilot.mp3";
String fileWithoutExtension = filename.substring(0, filename.lastIndexOf('.'));
System.out.println(Arrays.toString(fileWithoutExtension.replaceAll("[^\\w\\s]", "").split("\\s+")));
Output:
[01, Kodaline, Autopilot]

2 Comments

Why did you use replaceAll() in there? Care to elaborate?
Remove all the special characters from the string except numbers/characters/space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.