Java中正则表达式的实现_代码

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3. /**
  4. * @author jiakw
  5. * 正则表达式的使用
  6. */
  7. public class TestRegex {
  8.     /**
  9.     * 字符串匹配
  10.     * @param expression 正则表达式字符串
  11.     * @param text       要进行匹配的字符串
  12.     */
  13.     private static void matchingText(String expression, String text) {
  14.        Pattern p = Pattern.compile(expression); // 正则表达式
  15.        Matcher m = p.matcher(text); // 操作的字符串
  16.        boolean b = m.matches();
  17.        System.out.println(b);
  18.     }
  19.     /**
  20.     * 字符串替换
  21.     * @param expression 正则表达式字符串
  22.     * @param text       要进行替换操作的字符串
  23.     * @param str        要替换的字符串
  24.     */
  25.     private static void replaceText(String expression, String text, String str) {
  26.        Pattern p = Pattern.compile(expression); // 正则表达式
  27.        Matcher m = p.matcher(text); // 操作的字符串
  28.        String s = m.replaceAll(str);
  29.        System.out.println(s);
  30.     }
  31.     /**
  32.     * 字符串查找
  33.     * @param expression 正则表达式字符串
  34.     * @param text       要进行查找操作的字符串
  35.     * @param str        要查找的字符串
  36.     */
  37.     private static void findText(String expression, String text, String str) {
  38.        Pattern p = Pattern.compile(expression); // 正则表达式
  39.        Matcher m = p.matcher(text); // 操作的字符串
  40.        StringBuffer sb = new StringBuffer();
  41.        int i = 0;
  42.        while (m.find()) {
  43.             m.appendReplacement(sb, str);
  44.             i++;
  45.        }
  46.        m.appendTail(sb);
  47.        System.out.println(sb.toString());
  48.        System.out.println(i);
  49.     }
  50.     /**
  51.     * 字符串分割
  52.     * @param expression 正则表达式字符串
  53.     * @param text       要进行分割操作的字符串
  54.     */
  55.     private static void splitText(String expression, String text) {
  56.        Pattern p = Pattern.compile(expression); // 正则表达式
  57.        String[] a = p.split(text);
  58.        for (int i = 0; i < a.length; i++) {
  59.            System.out.println(a[i]);
  60.        }
  61.     }
  62.     /**
  63.     * @param args
  64.     */
  65.     public static void main(String[] args) {
  66.        matchingText("^card_([_0-9a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+)/?.shtml$""card_1020000000.shtml");
  67.        // 字符串匹配,这是不符合的
  68.        matchingText("a*b""baaaaab");
  69.        // 字符串匹配,这是符合的
  70.        matchingText("a*b""aaaaab");
  71.        // 字符串匹配,通用匹配
  72.        matchingText("^([_0-9a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+)/?""aaaaab");
  73.     
  74.        // 字符串替换
  75.        replaceText("ab""aaaaab""d");
  76.        replaceText("a*b""aaaaab""d");
  77.        replaceText("a*b""caaaaab""d");
  78.     
  79.        // 字符串查找
  80.        findText("cat""one cat two cats in the yard""dog");
  81.        findText("(fds){2,}""dsa da fdsfds aaafdsafds aaf""dog");
  82.       
  83.        // 字符串分割
  84.        splitText("a+""caaaaaat");
  85.        splitText("a+""c aa aaaa t");
  86.        splitText(" +""c aa    aaaa t");
  87.        splitText("//+""dsafasdfdsafsda+dsagfasdfa+sdafds");
  88.     }
  89. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值