[英]Regex String To Skip Text - Java
對於一個作業,我正在嘗試使用正則表達式解析一些 HTML,並且無法完全正確地獲取表達式。 我正在尋找一個城市組,路線 URL 和路線號。 我有一個字符串可以讓我知道城市,另一個可以讓我知道路線 URL 和路線編號,但我無法讓它們一起工作並跳過三個所需變量所在的標簽之間的文本。 這是我正在解析的站點 HTML 的示例:
<hr id="brier" />
<h3>Brier</h3>
<div class="row Community">
<div class="col-xs-3 text-nowrap">
<strong><a href="/schedules/route/111">111</a></strong>
</div>
<div class="col-xs-8 col-xs-offset-1">Brier to Mountlake Terrace</div>
</div>
<hr id="darrington" />
<h3>Darrington</h3>
<div class="row Community">
<div class="col-xs-3 text-nowrap">
<strong><a href="/schedules/route/230">230</a></strong>
</div>
<div class="col-xs-8 col-xs-offset-1">Darrington to Smokey Point</div>
</div>
<hr id="edmonds" />
我的第一個表達式在 H3 標記中捕獲城市名稱 (Brier):( ("<h3>(.*?)</h3>")
我的第二個捕獲了 href 之后的所有路由 URL 和數字: " <strong><a href=\"(.*?)\">(.*?)</a>"
我嘗試跳過它們之間的所有內容: (<h3>(.*?)</h3>.*?<strong><a href=\"(.*?)\">(.*?)</a>)
但它對我不起作用。 最終目標是使用 while 循環(Map<city, LinkedHashMap<routeNUM, routeURL>>)將它們加載到 Map 中,所以我想我還需要在表達式末尾添加一些東西來阻止它加載所有值到每個城市。
關於如何在一個表達式中實現所有這些的任何想法? 在下面發布我的實際代碼摘錄以供參考:
BufferedReader input = new BufferedReader(new InputStreamReader(routesURL.getInputStream()));
Map<String, LinkedHashMap<String, String>> outerMap = new HashMap<String, LinkedHashMap<String,String>>();
LinkedHashMap<String, String> innerMap = new LinkedHashMap<String, String>();
//Read site lines and add to String
while ((inputLine = input.readLine()) != null) {
text+= inputLine + "\n";
}
Pattern Pattern = Pattern.compile("<h3>(.*?)</h3>.*?<strong><a href=\"(.*?)\">(.*?)</a>");
Matcher Matcher = routePattern.matcher(text);
while (Matcher.find()) {
String city = Matcher.group(1);
String routeURL = Matcher.group(1);
String routeNum = Matcher.group(2);
System.out.println(routeNum + " " + fullURL);
//Add URL and routeNum to innerMap
outerMap.put(city, createInnerMap(routeNum, fullURL));
System.out.println(outerMap);
}
}
我願意將它保留在兩個表達式中,但是當我這樣做時,結果是它添加了第一個城市下的每條路線作為鍵,並停止而不是僅將正確的路線分配給每個城市,然后再次循環並添加next city 作為 outerMap 中的下一個鍵。 這是帶有兩個匹配器的代碼示例:
Pattern routePattern = Pattern.compile("<strong><a href=\"(.*?)\">(.*?)</a>");
Matcher routeMatcher = routePattern.matcher(text);
Pattern cityPattern = Pattern.compile("<h3>(.*?)</h3>");
Matcher cityMatcher = cityPattern.matcher(text);
while (cityMatcher.find()) {
String city = cityMatcher.group(1);
while (routeMatcher.find()) {
String routeURL = routeMatcher.group(1);
String routeNum = routeMatcher.group(2);
//Builds full route URL
String fullURL;
fullURL = URLtruncate + routeURL;
System.out.println(routeNum + " " + fullURL);
//Add URL and routeNum to innerMap
outerMap.put(city, createInnerMap(routeNum, fullURL));
System.out.println(outerMap);
}
}
BufferedReader input = new BufferedReader(new InputStreamReader(routesURL.getInputStream()));
List<String> lines = input.lines().collect(Collectors.toList());
Map<String, LinkedHashMap<String, String>> outerMap = new HashMap<String, LinkedHashMap<String,String>>();
LinkedHashMap<String, String> innerMap = new LinkedHashMap<String, String>();
Pattern Pattern1 = Pattern.compile("<h3>(.*?)</h3>");
Pattern Pattern2 = Pattern.compile("<strong><a href=\"(.*?)\">(.*?)</a></strong>");
for(int i = 0; i < lines.size(); i++){
Matcher Matcher = Pattern1.matcher(lines.get(i));
if(Matcher.find()){
String city = Matcher.group(1);
Matcher = Pattern2.matcher(lines.get(i + 3));
Matcher.find();
String routeURL = Matcher.group(1);
String routeNum = Matcher.group(2);
String fullURL = "full" + routeURL;
System.out.println(routeNum + " " + fullURL);
//Add URL and routeNum to innerMap
outerMap.put(city, createInnerMap(routeNum, fullURL));
System.out.println(outerMap);
i=i+4;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.