file io - Reading a specific text in Java -
this kind of followup other question simple java regex read between two
now code looks this. reading contents of file, scanning whatever between src , -t1. running code return 1 correct link source file contains 10 , can't figure out loop. thought way might write second file on disk , remove first link original source can't code either:
file workfile = new file("page.txt"); bufferedreader br = new bufferedreader(new filereader(workfile)); string line; while ((line = br.readline()) != null) { //system.out.println(line); string url = line.split("<img src=")[1].split("-t1")[0]; system.out.println(url); } br.close();
i think want like
import java.util.regex.*; pattern urlpattern = pattern.compile("<img src=(.*?)-t1"); while ((line = br.readline()) != null) { matcher m = urlpattern.matcher (line); while (m.find()) { system.out.println(m.group(1)); } }
the regular expression looks strings beginning <img src=
, ending -t1
(and looks shortest substrings possible, more 1 can found in line). part in parentheses "capture group" capture text gets matched; called group 1. then, each line, loop on find() find occurrences in each line. each time find one, print what's in group 1.
Comments
Post a Comment