37.正则表达式

在线正则表达式测试

image.png

image.png

image.png

image.png

image.png

image.png

image.png

训练题目:
1.匹配验证码(4位或者6位)
7777 888888
(^\d{4}) 对两个整体进行2选1

2.匹配车牌号
冀A-U888G 或 冀A-88888
^([冀黑粤冀][A-Z])-([0-9A-Z]{5})$

3.匹配中文(使用unicode码)
张三
^[\u4e00-\u9fa5]{2,8}$

2.表单验证





Insert title here


    
姓名:
身份证:

3页面提取

package com.imooc.regex;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexSample {
    public static void main(String[] args) {
        StringBuilder content = new StringBuilder();
        try {
            FileInputStream fis = new FileInputStream("D:\\software\\Eclipse\\eclipse workspace\\regex\\WebContent\\sample.html");
            InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
            BufferedReader bufferedReader = new BufferedReader(isr);
            String lineText = "";
            while((lineText = bufferedReader.readLine()) != null) {
//              System.out.println(lineText);
                content.append(lineText + "\n");
            }
            bufferedReader.close();
            System.out.println(content);
        
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        //1.创建正则表达式对象
        Pattern p = Pattern.compile("
  • ([\\u4e00-\\u9fa5]{2,10})([a-zA-Z]+)
  • "); //2.匹配正则表达式 Matcher m = p.matcher(content); //3.查找匹配的结果 while(m.find()) { // System.out.println(m.group(0)); String chs = m.group(1); String eng = m.group(2); System.out.println(chs + "-" + eng); } } }

    你可能感兴趣的:(37.正则表达式)