Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting String using RegEx in Android

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?

like image 370
Hadi Satrio Avatar asked Mar 07 '15 06:03

Hadi Satrio


People also ask

How to use regex to split a string?

To split a string by a regular expression, pass a regex as a parameter to the split() method, e.g. str. split(/[,. \s]/) . The split method takes a string or regular expression and splits the string based on the provided separator, into an array of substrings.

How do you split words on Android?

split() is documented with TextUtils. split(): String. split() returns [''] when the string to be split is empty.

What is split in Android?

On Android phones, Split Screen Mode allows you to view two apps on your phone at the same time. On Android phones, Split Screen Mode allows you to view two apps on your phone at the same time.


3 Answers

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.)

like image 59
ajb Avatar answered Oct 13 '22 02:10

ajb


this worked for me

str.split("\\.\\s+|\\s+-\\s+|\\.(mp3|flac)");
like image 43
Evgeniy Dorofeev Avatar answered Oct 13 '22 01:10

Evgeniy Dorofeev


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]
like image 41
SMA Avatar answered Oct 13 '22 03:10

SMA