Line break using RegEx on Android

I am trying to split Strings using RegEx without success. The idea is to break the metadata of the music file data into their file name so that:

 "01. Kodaline - Autopilot.mp3" 

.. will lead to ..

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

This is RegEx, which I am trying to use in its original form:

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

From what I read, I need to format RegEx for String.split(String regex) to work. So here is 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 do not get the expected result. Can you help me?

+5
source share
3 answers

Your regex is great for entering an input string. Your problem is that you used split() , which expects regex for a completely different purpose. For split() , the regex that you give it matches the delimiters (delimiters) that separate input parts; they do not match the entire input. So in a different situation (not in your situation) you could say

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

A regular expression matches a single character, which is a dash or space. This way it will look for dashes and spaces in your string and return parts separated by dashes and spaces.

To use your regular expression 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 lines "01" , " Kodaline " , " Autopilot" , "mp3" , which is close to what you want, except maybe for extra spaces (which you can search in your regular expression). Unfortunately, I do not think that there is a built-in Matcher function that returns all groups in a single array.

(By the way, in your regular expression you donโ€™t need backslashes before - , but they are harmless, so I left them. - usually does not really matter, so it does not need to be escaped. In square brackets, however, a hyphen is used so you have to use backslash if you want to match the character set and the hyphen is one of those so i used backslash in my split example above.)

+4
source

it worked for me

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

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] 
0
source

Source: https://habr.com/ru/post/1214871/


All Articles