gpt4 book ai didi

java - 为什么我的匹配器失败了?

转载 作者:行者123 更新时间:2023-11-29 10:08:11 25 4
gpt4 key购买 nike

我正在将一个字符串传递到我的歌曲解析器方法中,但它失败了,我不知道为什么。每件事都返回 null 或 0。

我的解析器方法是

 public static Song parseSong(String songString){
Map<String, String> songMap = new HashMap<String, String>();
Pattern pattern = Pattern.compile(".*<key>(.+)</key><(.+)>(.+)</.+>.*\n");
Scanner scanner = new Scanner(songString);
if(scanner.hasNext(pattern))
{
String line = scanner.next(pattern);
Matcher matcher = pattern.matcher(line);
MatchResult result = matcher.toMatchResult();
songMap.put(result.group(1), result.group(3));
}
int count = 0, rating = 0;
try{
count = Integer.parseInt(songMap.get("Play Count"));
}
catch(Exception e)
{
//bury this for now will handle when rest is working
}
try{
rating = Integer.parseInt(songMap.get("Rating"));
}
catch(Exception e)
{
//bury this for now will handle when rest is working
}
return new Song(songMap.get("Name"), songMap.get("Artist"), songMap.get("Album"),
songMap.get("Genre"), count, rating, songMap.get("Location"));

      String songString = "<key>Track ID</key><integer>160</integer>\n"+
"<key>Name</key><string>Ashley</string>\n"+
" <key>Artist</key><string>Escape the Fate</string>\n"+
" <key>Composer</key><string>Luca Gusella</string>\n"+
" <key>Album</key><string>This War Is Ours</string>\n"+
" <key>Genre</key><string>Metal</string>\n"+
"<key>Kind</key><string>AAC audio file</string>\n"+
" <key>Size</key><integer>7968219</integer>\n"+
" <key>Total Time</key><integer>246503</integer>\n"+
" <key>Track Number</key><integer>17</integer>\n"+
" <key>Year</key><integer>2005</integer>\n"+
" <key>Date Modified</key><date>2009-07-27T01:17:29Z</date>\n"+
" <key>Date Added</key><date>2009-07-27T01:17:00Z</date>\n"+
"<key>Play Count</key><integer>150</integer>\n"+
" <key>Bit Rate</key><integer>256</integer>\n"+
" <key>Sample Rate</key><integer>44100</integer>\n"+
" <key>Comments</key><string>\"Amanda\" performed by Aisha Duo from the CD Quiet Songs, courtesy of Obliq Sound. Written by Luca Gusella, published by Editions ObliqMusic (GEMA). All Rights Reserved. Used by Permission. </string>\n"+
" <key>Skip Count</key><integer>1</integer>\n"+
" <key>Skip Date</key><date>2009-07-27T01:46:32Z</date>\n"+
" <key>Artwork Count</key><integer>1</integer>\n"+
" <key>Persistent ID</key><string>A4D6F35FE9F41B58</string>\n"+
" <key>Track Type</key><string>File</string>\n"+
" <key>Location</key><string>file://localhost/C:/Documents%20and%20Settings/MB24244/Desktop/music/07%20Knees.m4a</string>\n"+
"<key>File Folder Count</key><integer>4</integer>\n"+
"afgjdhfshsgsughghanoise\n"+
"<key>Library Folder Count</key><integer>1</integer>\n"+
"<key>Rating</key><integer>100</integer>";

任何人都可以帮助解释我的方法有什么问题以及为什么这些小组不起作用(这似乎是问题所在)

最佳答案

为什么不使用XML 解析器解析XML

尽管查看 XML 示例,它并不是那么好,因为它本质上是在建模 map而不是建模 <song>

查看您的正则表达式,您为什么要查找以 \n 结尾的行.看来您正在依次匹配每一行,我不相信这些将包含换行符。

但是这种不使用扫描仪的方法是可行的。请注意,我已更改正则表达式以删除行结尾。

    Map<String, String> songMap = new HashMap<String, String>();

Pattern pattern = Pattern
.compile(".*<key>(.+)</key><(.+)>(.+)</.+>.*");

String[] lines = songString.split("\n");

for (String line : lines) {
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
songMap.put(matcher.group(1), matcher.group(3));
}
}

您也可以让它与扫描器一起使用。

关于java - 为什么我的匹配器失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1323128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com