Java 计算以x开头的字符串,并存储在Array Android Studio中
我制作了一个字符串regex,用于存储JSON数据,我可以在JSON数据中找到所有图像:Java 计算以x开头的字符串,并存储在Array Android Studio中,java,android,arrays,regex,json,Java,Android,Arrays,Regex,Json,我制作了一个字符串regex,用于存储JSON数据,我可以在JSON数据中找到所有图像: Pattern pattern = Pattern.compile("<a[^>]*>"); Matcher matcher = pattern.matcher(contentString.toString()); while(matcher.find()) { Log.i(TAG, "MATCHER : "+ matcher.gr
Pattern pattern = Pattern.compile("<a[^>]*>");
Matcher matcher = pattern.matcher(contentString.toString());
while(matcher.find()) {
Log.i(TAG, "MATCHER : "+ matcher.group());
}
Pattern=Pattern.compile(“]*>”);
Matcher Matcher=pattern.Matcher(contentString.toString());
while(matcher.find()){
Log.i(标记“MATCHER:+MATCHER.group());
}
返回:
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a rel="prettyPhoto[gallery-113]" href='http://www.bundoransurfco.com/wp-content/uploads/2014/11/april-13.jpg'>
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a rel="prettyPhoto[gallery-113]" href='http://www.bundoransurfco.com/wp-content/uploads/2014/11/10501752_10152650053307000_6249740615573255728_n1.jpg'>
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://www.windguru.cz/int/index.php?sc=103244">
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://www.xcweather.co.uk/">
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://www.buoyweather.com/wxnav6.jsp?region=UK&program=nww3BW1&grb=nww3&latitude=55.0&longitude=-8.75&zone=0&units=e">
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://www.windguru.cz/int/index.php?sc=103244">
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://www.xcweather.co.uk/">
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://www.buoyweather.com/wxnav6.jsp?region=UK&program=nww3BW1&grb=nww3&latitude=55.0&longitude=-8.75&zone=0&units=e">
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://magicseaweed.com/Bundoran-Surf-Report/50/">
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://magicseaweed.com/UK-Ireland-MSW-Surf-Charts/1/">
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://www.marine.ie/Home/site-area/data-services/marine-forecasts/wave-forecasts">
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://magicseaweed.com/Bundoran-Surf-Report/50/">
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://magicseaweed.com/UK-Ireland-MSW-Surf-Charts/1/">
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://www.marine.ie/Home/site-area/data-services/marine-forecasts/wave-forecasts">
04-13 16:33:57.280 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://news.bbc.co.uk/weather/forecast/13000">
04-13 16:33:57.280 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://www.met.ie/">
04-13 16:33:57.280 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://news.bbc.co.uk/weather/forecast/13000">
04-13 16:33:57.280 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://www.met.ie/">
04-13 16:33:57.280 3642-3657/jardelcompany.bundoransurfco I/MainActivity﹕ MATCHER : <a href="http://www.irishtimes.com/weather/tides.html">
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.279 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.280 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.280 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.280 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.280 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
04-13 16:33:57.280 3642-3657/jardelcompany.bundoransurfco I/main活动﹕ 匹配器:
但是现在我想计算以“
开头的链接数,我假设您只想在正则表达式中跳过[和]
所以你可以这样做:
<a rel="prettyPhoto\[gallery-113\]"[^>]*>
然后在循环中添加以下行:
list.add(matcher.group());
您不应该使用正则表达式解析HTML,而应该使用适当的解析器
- 像
rel
和href
这样的属性顺序可以更改,这样您就可以看到像这样的元素,下面是这个库的示例:
Document doc = Jsoup.parse(htmlText);
Elements relLinks = doc.select("a[rel]");
//or if you want rel only with "prettyPhoto[gallery-113]" use
//Elements relLinks = doc.select("a[rel=prettyPhoto[gallery-113]]");//
System.out.println("number of `rel`: "+relLinks.size());
for (Element el : relLinks){
System.out.println(el.attr("href"));
}
输出:
number of `rel`: 2
http://www.bundoransurfco.com/wp-content/uploads/2014/11/april-13.jpg
http://www.bundoransurfco.com/wp-content/uploads/2014/11/10501752_10152650053307000_6249740615573255728_n1.jpg
要向数组添加匹配项,请执行以下操作:
List<String> matches = new ArrayList<>();
while(matcher.find()) {
matches.add(matcher.group()); // or whatever you want to add
}
String[] array = matches.toArray();
String[] differentWay = matches.toArray(new String[matches.size()]);
你是在问如何生成那个正则表达式,或者如何在数组中存储匹配项,或者两者都是?在contentString.toString()
中,如果contentString
是字符串,那么toString()
是多余的。很好的正则表达式!!!谢谢:)现在,如果我想在数组中存储href的url,我该怎么做呢?:)
number of `rel`: 2
http://www.bundoransurfco.com/wp-content/uploads/2014/11/april-13.jpg
http://www.bundoransurfco.com/wp-content/uploads/2014/11/10501752_10152650053307000_6249740615573255728_n1.jpg
List<String> matches = new ArrayList<>();
while(matcher.find()) {
matches.add(matcher.group()); // or whatever you want to add
}
String[] array = matches.toArray();
String[] differentWay = matches.toArray(new String[matches.size()]);
String[] matches = new String[matcher.groupCount()];
index = 0;
while(matcher.find()) {
matches[index] = matcher.group();
index++;
}