Java Regular expression prints out matching data
Related knowledge points
Java regular match Chinese
Java regular match numbers
Java reads the text file
import java.io.bufferedreader;import java.io.file;import java.io.fileinputstream;import java.io.inputstreamreader;import java.util.hashset;import java.util.set;import Java.util.regex.matcher;import java.util.regex.pattern;public class prasesort0htm {public static void main (String[] args) {String regEx = "[\u4e00-\u9fa5]{1,}[ @#]{1,}/sort-0-[0-9]{1,}.htm "; // correctly matches Chinese string str = " Kiwi @#/sort-0-012345.htm, Orange @#/ Sort-0-012345.htm, Apple @#/sort-0-012345.htm, tropical fruit @#/sort-0-012345.htm, "; Pattern pat = pattern.compile (REGEX); Matcher mat = pat.matcher (str);while (Mat.find ()) {system.out.println (Mat.group (0));} System.out.println ("xxxxxxxxxxxxxxxxx"); string filepath = "D:/xxxx.txt"; Readtxtfile (FilePath);} Public static void readtxtfile (String filepath) {try {Set<String> Idset = new&nbSp Hashset<string> (); Set<string> nameset = new hashset<string> (); string regex = "[\u4e00-\u9fa5]{1,}[@#]{1,}/sort-0-[0-9]{1,}.htm"; // correctly matches string regEx2 = "[0-9]{2,}"; string regex3 = "[\u4e00-\u9fa5]{1,}"; Pattern pat = pattern.compile (REGEX); Pattern pat2 = pattern.compile (REGEX2); Pattern pat3 = pattern.compile (REGEX3); string encoding = "GBK"; File file = new file (FilePath);if (File.isfile () && file.exists ()) { // determine if a file exists Inputstreamreader read = new inputstreamreader (new FileInputStream (file), encoding);// Consider the encoding format bufferedreader bufferedreader = new BufferedReader (read); string linetxt = null;int i = 1;while ((linetxt = Bufferedreader.readline ()) != null) {// systEm.out.println (Linetxt); Matcher mat = pat.matcher (linetxt);while (Mat.find ()) {// system.out.println ( Mat.group (0));//validation can be printed successfully string temp = mat.group (0); Matcher mat2 = pat2.matcher (temp); string id = "";while (Mat2.find ()) {id = mat2.group (0);} if (Idset.add (ID)) {system.out.print (ID);} else {break;} System.out.print ("\ t"); Matcher mat3 = pat3.matcher (temp);while (Mat3.find ()) {system.out.print (Mat3.group (0) );} System.out.print ("\ n"); i++;}} system.out.println (i); Read.close ();} &NBSP;ELSE&NBSP;{SYSTEM.OUT.PRINTLN ("Cannot find the specified file");}} catch (exception e) {system.out.println ("Error reading file contents"); E.printstacktrace ();}}
This article from "Dream do not know is a guest" blog, declined reprint!
Java regular match