1. The java.util.regex API
Now that we've covered the theory of how to construct regular
expressions, the
hard part is over. All that's left is to investigate the Java API for applying
regexes: searching for them in strings, retrieving captured text, and replacing
matches with substitution text.
1.1 Pattern
As we've said, the regex patterns that we write as strings are,
in actuality, little programs describing how to match text. At runtime, the Java
regex package compiles these little programs into a form that it can execute
against some target text. Several simple convenience methods accept strings
directly to use as patterns. More generally, however, Java allows you to
explicitly compile your pattern and encapsulate it in an instance of a
Pattern
object. This is the most efficient way to handle patterns that
are used more than once, because it eliminates needlessly recompiling the
string. To compile a pattern, we use the static method Pattern.compi ......
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class DisplayFile {
/**
* @param args
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
JFrame frame =new JFrame("Display File");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea ta=new JTextArea(20,30);
  ......
———————————————————
¸¶³öµÄŬÁ¦·Ç³£ÈËËùÄÜÏëÏó
————————————————————
ÄÇÎÒ¿ÉÒԿ϶¨µÄ˵£¬100¸ö³ÌÐòÔ±ÀïÖÁÉÙÓÐ10¸öÈ˸¶³öµÄŬÁ¦·Ç³£ÈËÏëÏ󣬵«ÊǼá³Ö³¬¹ýÈýÄêµÄ²»»á³¬¹ý5¸ö£¬³¬¹ý5ÄêµÄ²»»á³¬¹ý1¸ö
01Ä꿪ʼµ½ÏÖÔÚ¼ûµÄ³ÌÐòÔ±¶àÁË£¬ÈÏʶµÄÒ²¶àÁË£¬ÏñÂ¥Ö÷ÕâÑùÓоõÎòµÄÒ²¶àÁË£¬ÉõÖÁ¹¤×÷°ëÄêºóÓÐÂ¥Ö÷ÕâÑù¾õÎòµÄÈËÒ²¶àÁË¡£
ÔÚÕâÀï²¢²»ÊǰÚ×ʸñ£¬ËµÊ²Ã´01Äê 00Ä꣬ÔÚ±à³ÌÉÏÎÒ»¹²ÅÈëÃÅ£¬¾¡¹ÜÕâ5ÄêÀïÎÒÿÄêÐÝÏ¢µÄʱ¼äºÜÉÙ³¬¹ý10Ì죬ÿÌìºÜÉÙÁ賿1µãǰ˯¾õ£¨Á½Äêǰ¶¼ÊÇÁ賿2µã˯¾õ£©£¬¾¡¹ÜÎÒÖªµÀÒÔǰдµÄ´úÂë´ó²¿·Ö¶¼ÊÇÀ¬»ø£¬¾¡¹ÜÎÒÏÖÔÚ¿ÉÒÔ˵£¬ÎÒд³öµÄ´úÂë¾ø¶Ô²»ÊÇÀ¬»ø£¬» ......
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Notepad extends JFrame {
// ϵͳ×é¼þÉùÃ÷
private JMenuBar menuBar = new JMenuBar();
private JEditorPane content = new JEditorPane();
private JScrollPane scroll = new JScrollPane(content);
private JFileChooser filechooser = new JFileChooser() ;
private BorderLayout bord = new BorderLayout();
private JLabel statusBar = new JLabel();
private JPanel pane = new JPanel();
private File file = null;
// ¶¨ÒåÎļþ²Ëµ¥
private JMenu fileMenu = new JMenu();
private JMenuItem newMenuItem = new JMenuItem();
private JMenuItem openMenuItem = new JMenuItem();
private JMenuItem saveMenuItem = new JMenuItem();
private JMenuItem saveAsMenuItem = new JMenuItem();
private JMenuItem pageSetupMenuItem = new JMenuItem();
private JMenuItem printMenuItem = new JMenuItem();
private JMenuItem exitMenuItem = new JMenuItem();
// ¶¨Òå·ç¸ñ²Ëµ¥
private JMenu styleMenu = new JMen ......
import java.awt.*;
import java.awt.event.*;
public class MyCalculator {
private Frame f;
private TextField tf = new TextField(30);
private long result;
private boolean append=false;
private char operator='=';
private Button[] btn=new Button[15];
public MyCalculator() {
initComponent();
}
private void initComponent() {
f = new Frame("My Calculator V1.0");
f.setLayout(new BorderLayout()); //The frame uses BorderLayout
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
&nb ......
UTF8ת»»³ÉGB2312 µ±ÎÒÃÇÔÚ»ùÓÚHTTPÐÒéµÄJSP»òServletµÄÓ¦ÓÃÖлñÈ¡Êý¾Ý»ò·¢ËÍÇëÇóʱ£¬JVM»á°ÑÊäË͵ÄÊý¾Ý±àÂë³ÉUTF8¸ñʽ¡£Èç¹ûÎÒÃÇÖ±½Ó´ÓHTTPÁ÷ÖÐÌáÈ¡ÖÐÎÄÊý¾Ý£¬ÌáÈ¡µÄ½á¹ûΪ“????”£¨¿ÉÄܸü¶àÎʺţ©£¬Îª×ª»»³ÉÎÒÃÇÄܹ»Àí½âµÄÖÐÎÄ×Ö·û£¬ÎÒÃÇÐèÒª°ÑUTF8ת»»³ÉGB2312£¬½èÖúISO-8859-1±ê×¼±àÂëÄܹ»ÇáÒ×µÄʵÏÖ£¬ÏÂÃæµÄ´úÂëʵÏÖÁËÕâÒ»¹¦ÄÜ£º
byte [] b;
String utf8_value;
utf8_value = request.getParameter("NAME");//´ÓHTTPÁ÷ÖÐÈ¡"NAME"µÄUTF8Êý¾Ý
b = utf8_value.getBytes("8859_1"); //ÖмäÓÃISO-8859-1¹ý¶É
String name = new String(b, "GB2312"); //ת»»³ÉGB2312×Ö·û
¡¡¡¡ÔÚÖªµÀÁ÷³¤¶ÈµÄÇé¿öϽ«ÊäÈëÁ÷ת»»³É×Ö½ÚÊý×é JavaÖеÄÊäÈëÁ÷³éÏóÀàInputStreamÓÐint read(byte[] b, int off, int len)·½·¨£¬²ÎÊýÖÐbyte[] bÊÇÓÃÀ´´æ·Å´ÓInputStreamÖжÁÈ¡µÄÊý¾Ý,int offÖ¸¶¨Êý×ébµÄÆ«ÒÆµØÖ·£¬Ò²¾ÍÊÇÊý×ébµÄÆðʼϱ꣬int lenÖ¸¶¨ÐèÒª¶ÁÈ¡µÄ³¤¶È£¬·½·¨·µ»ØÊµ¼Ê¶ÁÈ¡µÄ×Ö½ÚÊý¡£¸ÕѧJavaµÄÅóÓÑ¿ÉÄÜҪ˵£ºÏȶ¨ÒåÒ»¸öÓëÁ÷³¤¶ÈµÈ³¤µÄ×Ö½ÚÊý×飬µ÷ÓÃread·½·¨£¬Ö¸¶¨ÆðʼϱêΪ0£¬Ö¸¶¨¶ÁÈ¡³¤¶ÈÓëÊý×鳤¶ÈµÈ³¤£¬²»ÊÇÒ»ÏÂ×Ó¿ÉÒÔ¶Á³öÀ´ÁËÂð£¿ËµµÄû´í£¬±ÊÕßÔø¾Ò²ÊÔ×ÅÕâÑù¶ÁÈ ......