Java Regular Expression Application Java read the file and get the phone number _java

Source: Internet
Author: User

implementation function: read the file, put the phone number in a set return.

Method Introduction:

Find (): attempts to locate the next subsequence of the input sequence that matches the pattern.

Group (): returns the input subsequence matched by the previous matching operation.

1. Get the phone number from a string

Import Java.util.HashSet;
Import Java.util.Set;
Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern;  /** * Intercepts the phone number from the string * @author ZCR * */public class Checkifisphonenumber {/** * Regular expression for phone number: including fixed telephone and mobile phone *     Compliant Number: * 1, mobile phone * 86+ '-' + 11-digit phone number * 86+11 normal phone number * 11-bit normal phone number A * (+86) + 11-digit phone number * (86) + 11-bit phone number * 2, fixed telephone * area code + '-' + fixed telephone + '-' + extension # * Area code + '-' + fixed telephone * Area code + fixed Telephone * @
    
    Return phone number Regular expression */public static string Isphoneregexp () {String regexp = ""; Can meet the longest match, but cannot complete the space between the country area number and telephone number String mobilephoneregexp = "(?:( \ \ (\\+?86\\)) ((13[0-9]{1}) | ( 15[0-9]{1}) | (18[0,5-9]{1})) +\\D{8}) | "+" (?: 86-? ( (13[0-9]{1}) | (15[0-9]{1}) | (18[0,5-9]{1})) +\\D{8}) | "+" (?:( (13[0-9]{1}) | (15[0-9]{1}) | (18[0,5-9]{1}))
    
    
    
    +\\D{8}) ";
    System.out.println ("regexp =" + mobilephoneregexp); Fixed-line Regular expression String landlinephoneregexp = "(?:( \\(\\+?86\\)) (0[0-9]{2,3}\\-?)? ([2-9][0-9]{6,7}) + (\\-[0-9]{1,4}) | " +
        "(?:( 86-?)? (0[0-9]{2,3}\\-?)?  

    ([2-9][0-9]{6,7}) + (\\-[0-9]{1,4}) "; 
  
    RegExp = "(?:" + Mobilephoneregexp + "|" + Landlinephoneregexp + ")";
  return regexp;
   /** * Obtain all phone numbers (fixed and mobile) from Datastr, put them into set * @param datastr the string to look for * @param the phone number in Phoneset datastr 
    /public static void Getphonenumfromstrintoset (String datastr,set<string> phoneset) {//Get regular expressions for fixed phone and mobile phone
    
    String regexp = Isphoneregexp ();
    
    System.out.println ("Regexp =" + Regexp); 
    Pattern pattern = pattern.compile (regexp); 

    Matcher Matcher = Pattern.matcher (DATASTR); Find the next subsequence of the input sequence that matches the pattern while (Matcher.find ()) {//Get the previously found string and add it to the set Phoneset.add (Matcher.group) (
    ));
  }//system.out.println (Phoneset);
 }
}

2, read the file and call the phone number to obtain the
implementation: read the file by the file path, and fetch the phone number in the line

Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.InputStreamReader;
Import java.util.ArrayList;
Import Java.util.HashSet;
Import java.util.List;



Import Java.util.Set;
   /** * Reads the file operation * * * @author ZCR * */public class ImportFile {/** * reads the file, reads the phone number in the file, and saves it in the set.  * @param the absolute path of the FilePath file * @return The phone number contained in the file/public static set<string> Getphonenumfromfile (String
    
    FilePath) {set<string> phoneset = new hashset<string> ();
      try {String encoding = "UTF-8";
      File File = new file (FilePath);
            if (File.isfile () && file.exists ()) {//To determine if the file exists inputstreamreader read = new InputStreamReader ( 
        New FileInputStream (file), encoding)//Considering the encoding grid BufferedReader BufferedReader = new BufferedReader (read);

        String linetxt = null; while ((Linetxt = Bufferedreader.readline ())!= null) {//read a row in the file, the electricThe words number is added to the Phoneset checkifisphonenumber.getphonenumfromstrintoset (Linetxt, Phoneset);
      } read.close ();
      else {System.out.println ("The specified file cannot be found");
      The catch (Exception e) {System.out.println ("Error reading file contents");
    E.printstacktrace ();
  return phoneset;
 }

}

3, testing

public static void Main (String argv[])
  {
    string filePath = "F:\\three.txt";  
    
    set<string> Phoneset = Getphonenumfromfile (FilePath);
    
    SYSTEM.OUT.PRINTLN ("Telephone set:" + Phoneset);
  }

Data in File:

Results:

Telephone set: [86132221, (86) 13222144332, 86-13222144332, 32434343, (+86) 13222144332, 13888888888]

The above is the implementation of the entire application process, I hope you through this case, the Java regular expression used more skilled.

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.