Jad*_*eld 5 java regex android
我有一个ListView,显示歌曲名称和艺术家.有时,歌曲名称包含曲目编号和分隔符,其中包含一些格式
13 - J. Cole - Best Friend
Run Code Online (Sandbox Code Playgroud)
和其他人一样
13 - Beautiful People
Run Code Online (Sandbox Code Playgroud)
经过一番挖掘后,我发现最好的方法是定义一个正则表达式模式,它将删除字符串中的任何不必要的字符.凉.我在这里查看了类似主题的其他SO问题和一些博客文章,但仍然没有运气.
这是我第一次处理正则表达式时,我发现它非常有用,只是试图提出有效/有用的模式.
如果是匹配的话,这就是我需要从字符串中删除的内容
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)
Run Code Online (Sandbox Code Playgroud)
任何有关这方面的帮助都会像往常一样非常感谢,谢谢!
编辑:我想要的相同输出是这样的,只有歌曲名称.没有曲目编号,如果适用; " - "后面没有艺术家的名字
Beautiful People
Angel of Mine
Human Nature
Run Code Online (Sandbox Code Playgroud)
这是一个可以使用的正则表达式:
name.replaceFirst("^\\d+\\.?\\s*-(?:.*?-)?\\s*", "")
Run Code Online (Sandbox Code Playgroud)
这取出: