简体   繁体   English

在Android中使用RegEx拆分字符串

[英]Splitting String using RegEx in Android

I've been trying to split Strings using RegEx with no success. 我一直试图使用RegEx拆分Strings没有成功。 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: 这是RegEx我一直试图以其原始形式使用:

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

From what I've read, I need to format the RegEx for String.split(String regex) to work. 根据我的阅读,我需要格式化String.split(String regex)的RegEx才能工作。 So here's my formatted RegEx: 所以这是我格式化的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? 你能帮帮我吗?

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. 您的问题是您使用了split() ,它期望正则表达式具有完全不同的目的。 For split() , the regex you give it matches the delimiters (separators) that separate parts of the input; 对于split() ,你给它的正则表达式与分隔输入部分的分隔符 (分隔符)相匹配; 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). 它将metadata设置为字符串"01"" Kodaline "" Autopilot""mp3" ,这是您想要的,除了可能是额外的空格(您可以在正则表达式中查找)。 Unfortunately, I don't think there's a built-in Matcher function that returns all the groups in one array. 不幸的是,我不认为有一个内置的Matcher函数可以返回一个数组中的所有组。

(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.) (顺便说一句,在你的正则表达式中,你不需要前面的反斜杠- ,但它们是无害的,所以我把它们留在了里面。 -通常没有特殊含义,所以它不需要但是,在方括号内,连字符特殊的,所以如果你想匹配一组字符并且连字符是其中一个字符,你应该使用反斜杠。这就是我在上面的split示例中使用反斜杠的原因。)

这对我有用

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

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM