I have a ListView that display song names and artists. Sometimes, the song names contain the track number and a separator with some in this format
我有一个ListView,显示歌曲名称和艺术家。有时,歌曲名称包含曲目编号和分隔符,其中包含一些格式
13 - J. Cole - Best Friend
and others like this
和其他人一样
13 - Beautiful People
After some digging around I found that the best way to go about this is to define a regex pattern that will remove any unnecessary characters in the string. Cool. I've looked at other SO questions on a similar topic here and a couple blog posts but still no luck.
经过一番挖掘后,我发现最好的方法是定义一个正则表达式模式,它将删除字符串中的任何不必要的字符。凉。我在这里查看了类似主题的其他SO问题以及一些博客文章,但仍然没有运气。
This my first time dealing with regular expressions and I find it quite useful, just trying to wrap my head around coming up with efficient/useful patterns.
这是我第一次处理正则表达式,我发现它非常有用,只是试图提出有效/有用的模式。
Here's what I need to remove from the string if it is a match
如果是匹配的话,这就是我需要从字符串中删除的内容
The track number
The "-" or whatever separator character that follows it
The artist name and the "-" that follows that(Each artist name is listed below the song, so it would be redundant)
Any help on this would be greatly appreciated as usual guys, thanks!
任何有关这方面的帮助都会像往常一样非常感谢,谢谢!
Edit: The same output that I would like is something like this, with just the song names. No track numbers, and if applicable; no artist names following the "-"
编辑:我想要的相同输出是这样的,只有歌曲名称。没有曲目编号,如果适用的话; “ - ”后面没有艺术家姓名
Beautiful People
Angel of Mine
Human Nature
3 个解决方案
#1
5
Here's a possible regex you could use:
这是一个可以使用的正则表达式:
name.replaceFirst("^\\d+\\.?\\s*-(?:.*?-)?\\s*", "")
This takes out:
这取出:
- digits at the front
- optionally followed by a dot
- optionally spaces
- a hyphen
- if a further hyphen is found, then everything up to that
- optionally spaces
前面的数字
可选地后跟一个点
如果找到另一个连字符,那么一切都是如此
#2
2
A piece of code will be better to give you a hint:
一段代码会更好地给你一个提示:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String[] songNames = {
"1 - my awesome artist - go beyond",
"2 - such is life",
"My awesome song"
};
Pattern trackNumberPattern = Pattern.compile("^ *\\d+ *- *");
Pattern artistPattern = Pattern.compile(" *- *[^-]+ *- *");
Matcher matcher;
for(String song: songNames) {
matcher = trackNumberPattern.matcher(song);
if(matcher.find()) {
song = matcher.replaceFirst("");
}
matcher = artistPattern.matcher(song);
if(matcher.find()) {
song = matcher.replaceFirst("");
}
System.out.println(">>> " + song);
}
}
}
You can find the doc to write regex in java here: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
你可以在这里找到用java编写正则表达式的文档:http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
DISCLAIMER: This won't work properly if some of the song names or artist names contain dashes (-), etc...
免责声明:如果某些歌曲名称或艺术家名称包含破折号( - )等,这将无法正常工作......
#3
1
You can use this regex (a little bit radical):
你可以使用这个正则表达式(有点激进):
^\\d*+\\W*+\\b(?>[^-]++-\\s++(?=\w))?
and replace it by nothing.
并取而代之的是什么。
this will destroy all digits followed by non-word characters at the begining, and the interpret name if it is present
这将破坏所有数字,后跟开头的非单词字符,以及解释名称(如果存在)
#1
5
Here's a possible regex you could use:
这是一个可以使用的正则表达式:
name.replaceFirst("^\\d+\\.?\\s*-(?:.*?-)?\\s*", "")
This takes out:
这取出:
- digits at the front
- optionally followed by a dot
- optionally spaces
- a hyphen
- if a further hyphen is found, then everything up to that
- optionally spaces
前面的数字
可选地后跟一个点
如果找到另一个连字符,那么一切都是如此
#2
2
A piece of code will be better to give you a hint:
一段代码会更好地给你一个提示:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String[] songNames = {
"1 - my awesome artist - go beyond",
"2 - such is life",
"My awesome song"
};
Pattern trackNumberPattern = Pattern.compile("^ *\\d+ *- *");
Pattern artistPattern = Pattern.compile(" *- *[^-]+ *- *");
Matcher matcher;
for(String song: songNames) {
matcher = trackNumberPattern.matcher(song);
if(matcher.find()) {
song = matcher.replaceFirst("");
}
matcher = artistPattern.matcher(song);
if(matcher.find()) {
song = matcher.replaceFirst("");
}
System.out.println(">>> " + song);
}
}
}
You can find the doc to write regex in java here: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
你可以在这里找到用java编写正则表达式的文档:http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
DISCLAIMER: This won't work properly if some of the song names or artist names contain dashes (-), etc...
免责声明:如果某些歌曲名称或艺术家名称包含破折号( - )等,这将无法正常工作......
#3
1
You can use this regex (a little bit radical):
你可以使用这个正则表达式(有点激进):
^\\d*+\\W*+\\b(?>[^-]++-\\s++(?=\w))?
and replace it by nothing.
并取而代之的是什么。
this will destroy all digits followed by non-word characters at the begining, and the interpret name if it is present
这将破坏所有数字,后跟开头的非单词字符,以及解释名称(如果存在)