Java/Regular Expressions/Serialization

Материал из Java эксперт
Перейти к: навигация, поиск

Apply Regular Expressions on the contents of a file

 
 
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String[] argv) throws Exception {
    Pattern pattern = Pattern.rupile("pattern");
    FileInputStream input = new FileInputStream("file.txt");
    FileChannel channel = input.getChannel();
    ByteBuffer bbuf = channel.map(FileChannel.MapMode.READ_ONLY, 0, (int) channel.size());
    CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf);
    Matcher matcher = pattern.matcher(cbuf);
    while (matcher.find()) {
      String match = matcher.group();
      System.out.println(match);
    }
  }
}





Reading Lines from a String Using a Regular Expression

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String[] argv) throws Exception {
    CharSequence inputStr = "a\rb"; 
    inputStr = "a\r\nb"; 
    inputStr = "a\nb"; 
    String patternStr = "^(.*)$";
    Pattern pattern = Pattern.rupile(patternStr, Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(inputStr);
    while (matcher.find()) {
      String lineWithTerminator = matcher.group(0);
      String lineWithoutTerminator = matcher.group(1);
    }
  }
}





Use FileChannels and ByteBuffers to Store Patterns

//Example File
/*       
#Email validator that adheres directly to the specification
#for email address naming. It allows for everything from
#ipaddress and country-code domains to very rare characters
#in the username.
email=^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-
9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-
9]{1,3})(\]?)$
#Matches UK postcodes according to the following rules 1. LN NLL
#eg N1 1AA 2. LLN NLL eg SW4 0QL 3. LNN NLL eg M23 4PJ 4. LLNN NLL
#eg WS14 0JT 5. LLNL NLL eg SW1N 4TB 6. LNL NLL eg W1C 8LQ Thanks
#to Simon Bell for informin ...
zip=^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$
#This regular expression matches dates of the form XX/XX/YYYY
#where XX can be 1 or 2 digits long and YYYY is always 4
#digits long.
dates=^\d{1,2}\/\d{1,2}\/\d{4}$
*/
import java.util.Properties;
import java.util.regex.*;
import java.util.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.logging.Logger;
public class RegexProperties extends Properties {
  private static Logger log = Logger.getAnonymousLogger();
  public void load(String inStream) throws IOException,
      PatternSyntaxException {
    load(new FileInputStream(inStream));
  }
  public void load(FileInputStream inStream) throws IOException,
      PatternSyntaxException {
    FileChannel fc = inStream.getChannel();
    ByteBuffer bb = ByteBuffer.allocate((int) fc.size());
    fc.read(bb);
    bb.flip();
    String fileContent = new String(bb.array());
    Pattern pattern = Pattern.rupile("^(.*)$", Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(fileContent);
    while (matcher.find()) {
      String line = matcher.group(1);
      if (line != null && !"".equals(line.trim())
          && !line.startsWith("#") && !line.startsWith("!")) {
        String keyValue[] = null;
        if (line.indexOf("=") > 0)
          keyValue = line.split("=", 2);
        else
          keyValue = line.split(":", 2);
        if (keyValue != null) {
          super.put(keyValue[0].trim(), keyValue[1]);
        }
      }
    }
    fc = null;
    bb = null;
  }
  public void store(FileOutputStream out, String header)
      throws UnsupportedOperationException {
    throw new UnsupportedOperationException("unsupported for this class");
  }
  public void putAll(Map t) {
    throw new UnsupportedOperationException("unsupported for this class");
  }
}





Using a Regular Expression to Filter Lines from a Reader

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String[] argv) throws Exception {
    String filename = "infile.txt";
    String patternStr = "pattern";
    BufferedReader rd = new BufferedReader(new FileReader(filename));
    Pattern pattern = Pattern.rupile(patternStr);
    Matcher matcher = pattern.matcher("\\D");
    String line = null;
    while ((line = rd.readLine()) != null) {
      matcher.reset(line);
      if (matcher.find()) {
        // line matches the pattern
      }
    }
  }
}