如何解析字符串以在Java中映射[关闭]

问题描述 投票:-2回答:1

如何在java中为下面的字符串编写解析器

"SiteId:BLR232#Latitude:12.918444#Longitude:77.5940136#NetworkType:4g#Type:NONE#Type of Complaint:Call Drop#Sample Number:7022979575#Problem Description:agshjs vshsijsb#"

因此结果输出将在Java的hashmap中>

Key = SiteId, Value = BLR232
Key = Type, Value = NONE
Key = Problem Description, Value = agshjs vshsijsb
Key = Sample Number, Value = 7022979575
Key = NetworkType, Value = 4g
Key = Latitude, Value = 12.918444
Key = Type of Complaint, Value = Call Drop
Key = Longitude, Value = 77.5940136

我曾尝试使用Pattern p = Pattern.compile("(\\w+):(\\w+)");,但不能完全满足我的需要。

如何在java“ SiteId:BLR232#Latitude:12.918444#Longitude:77.5940136#NetworkType:4g#Type:NONE#投诉类型:Call Drop#Sample Number:7022979575#Problem中为以下字符串编写解析器:...

java string-parsing
1个回答
2
投票

您的(\w+):(\w+)仅匹配:之前和之后的单个单词。您将#作为记录定界符,并将:作为键值定界符,因此不能仅依赖\w类。

© www.soinside.com 2019 - 2024. All rights reserved.