java - Regex and find in text file -
i have code :
package ggg; import java.util.regex.matcher; import java.util.regex.pattern; import java.io.*; public class regex { public static void main( string args[] ){ // string scanned find pattern. string line = "this order placed fro-dda-6666666 %10.25 %10.12 fro-dda-8888888 qt3000! ok?"; string pattern = "\\d+\\.\\d{2}"; string pattern2 = "\\d{7}"; // create pattern object pattern r = pattern.compile(pattern); pattern t = pattern.compile(pattern2); // create matcher object. matcher m = r.matcher(line); matcher g = t.matcher(line); try { printwriter writer = new printwriter("c:\\users\\john\\workspace\\ggg\\src\\ggg\\text.txt", "utf-8"); (int = 1; m.find() && g.find(); i++) { writer.println(g.group(0)+"->"+m.group(0)); } writer.close(); } catch (ioexception ex) {} } }
and result is:
6666666->10.25
8888888->10.12
i want write simple code read text.txt file , if "8888888" exist in file print front of "8888888->", should ?
example in our result 10.12 front of 888888->
add code after building file:
bufferedreader br = new bufferedreader(new filereader("c:\\users\\john\\workspace\\ggg\\src\\ggg\\text.txt")); string myline; while ((myline = br.readline()) != null) { if(myline.contains("8888888")) system.out.println(myline.substring(myline.indexof(">")+1)); } br.close();
Comments
Post a Comment