Java regular match

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.