[英]RSS FEED - data parsing
如何從以下解析的數據中檢索位置?
<description>Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0</description>
此詳細信息在描述標記中,並且描述已被解析為數組列表。 如何從中獲取位置?
您可以使用正則表達式(?<=Location: ).*?(?=;)
來查找並提取所需的匹配項。
使用Stream
API 的解決方案:
import java.util.List;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String str = "<description>Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0</description>";
List<String> list = Pattern.compile("(?<=Location: ).*?(?= ;)")
.matcher(str)
.results()
.map(MatchResult::group)
.collect(Collectors.toList());
System.out.println(list);
}
}
Output:
[BLACKFORD,PERTH/KINROSS]
非Stream
解決方案:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String str = "<description>Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0</description>";
Matcher matcher = Pattern.compile("(?<=Location: ).*?(?= ;)").matcher(str);
List<String> list = new ArrayList<>();
while (matcher.find()) {
list.add(matcher.group());
}
System.out.println(list);
}
}
Output:
[BLACKFORD,PERTH/KINROSS]
regex101正則表達式的解釋:
如果你得到的只是
Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0
您將不得不(a)確定規定此格式的標准(如果有)或(b)自己做,即查看結構並決定基於此進行解析。
看來您可以使用分隔符“;”對字符串使用 split() 方法。 那應該給你一個長度為 5 的數組。
然后,您可以假設 Location 始終位於第二個 position 中,或者簡單地遍歷數組,直到找到以 Location 開頭的字符串。
public class Location {
public static void main(String[] args) {
String rawData = "Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0\r\n";
String[] dataArray = rawData.split(" ; ");
System.out.println(dataArray[1]);
}
}
或者,您可以使用一個正則表達式,它可以直接為您提供值,而無需執行我剛才描述的步驟。 您要查找的值始終以Location:
開頭並以;
結尾看看這本入門書就可以開始了。
Pattern pattern = Pattern.compile("(?<=Location: ).*?;", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(rawData);
boolean matchFound = matcher.find();
if(matchFound) {
System.out.println("Match found: "+matcher.group());
} else {
System.out.println("Match not found");
}
使用字典和正則表達式:
string pattern = @"(?'key'[^:]+):\s+(?'value'.*)";
string input = "Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0";
string[] splitArray = input.Split(new char[] { ';' });
Dictionary<string, string> dict = splitArray.Select(x => Regex.Match(x, pattern))
.GroupBy(x => x.Groups["key"].Value.Trim(), y => y.Groups["value"].Value.Trim())
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
string location = dict["Location"];
或這個
string pattern = @"(?'key'[^:]+):\s+(?'value'[^;]+);?";
string input = "Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0";
string[] splitArray = input.Split(new char[] { ';' });
MatchCollection matches = Regex.Matches(input, pattern);
Dictionary<string, string> dict = matches.Cast<Match>()
.GroupBy(x => x.Groups["key"].Value.Trim(), y => y.Groups["value"].Value.Trim())
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
string location = dict["Location"];
嘗試
String desc = "Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0";
String[] parts = desc.split(";");
for ( String part : parts )
{
if ( part.contains("Location") )
{
parts = part.split(":");
System.out.println("***************** Location is: '" + parts[1].trim() + "'");
break;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.