1.1 正则表达式【匹配单个字符】

目录

 

单字符匹配符  . 

测试实例1

测试实例2【配置特殊字符.】

测试代码【Java】:

编程技术



单字符匹配符  . 

  . 匹配字符可以匹配任意一个字符。 . 例如 : a,b,c , . ,'等。

测试实例1

被匹配字符串 

private final static String testStr1 = "This is a matched string";

匹配公式1:

 String pattern = "is";

匹配公式2:

 String pattern2 = ".is";

匹配公式3:

 String pattern3 = ".is.";

匹配公式4:

String pattern4 = ".is..";

匹配结果:


测试实例2【配置特殊字符.】

被匹配字符串:

private final static String testStr3 = "This is a matched string 【isa.bbb】";

匹配公式:【\\第一个是转译 第二个 \ 字符,第二个字符转译 . 】

 String pattern5 = ".is.\\..";

匹配结果:

可以 使用 \ 转译 . 字符,此时的 . 不再是正则表达式的单字符匹配符 ,而是普通的字符 .  

 

下面有更加明显的测试结果:

测试代码【Java】:

package com.kgo.javaregular.character;

import cn.hutool.core.util.ReUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author keepgoon
 * @Description: TODO
 * @date 2019/9/3021:47
 */
public class SingleCharacter {
    private static Logger log = LoggerFactory.getLogger(SingleCharacter.class);
    private final static String testStr1 = "This is a matched string";
    private final static String testStr2 = "this is test character string \n this is next str";
    private final static String testStr3 = "This is a matched string 【isa.bbb】";
    private final static String testStr4 = "This is a matched string 【isa*bbb】";
    public static void main(String[] args) {
        String pattern = "is";
        String pattern2 = ".is";
        String pattern3 = ".is.";
        String pattern4 = ".is..";
        String pattern5 = ".is.\\..";
        String pattern6 = ".is.\\*.";
        log.debug("===============匹配公式1 =======================");
        find(pattern,testStr1);
        log.debug("\n==============匹配公式2 =========================");
        find(pattern2,testStr1);
        log.debug("\n==============匹配公式3 =========================");
        find(pattern3,testStr1);
        log.debug("\n==============匹配公式4 =========================");
        find(pattern4,testStr1);
        log.debug("\n==============匹配公式 特殊字符  . =========================");
        find(pattern5,testStr3);
        log.debug("\n==============匹配公式 特殊字符  . =========================");
        find(pattern6,testStr3);
        log.debug("\n==============匹配公式 特殊字符  . =========================");
        find(pattern6,testStr4);

    }
    private static String length(String printStr){
      return  StringUtils.rightPad(printStr,15," ") + " : {}";
    }
    private static void find(String pattern,String matchedStr){
        log.debug(" 正则表达式 【 {} 】; 被匹配的字符串 【 {} 】" ,pattern,matchedStr);
        log.debug(length("findAll"),ReUtil.findAll(pattern,matchedStr,0));
        log.debug(length("count"), ReUtil.count(pattern,matchedStr));
        log.debug(length("contains"), ReUtil.contains(pattern,matchedStr));
        //log.debug(length("contains"), ReUtil.get);
    }
    private static void find(Pattern pattern,String matchedStr){
        log.debug(" 正则表达式 【 {} 】; 被匹配的字符串 【 {} 】" ,pattern,matchedStr);
        log.debug(length("findAll"),ReUtil.findAll(pattern,matchedStr,0));
        log.debug(length("count"), ReUtil.count(pattern,matchedStr));
        log.debug(length("contains"), ReUtil.contains(pattern,matchedStr));
        //log.debug(length("contains"), ReUtil.get);
    }
}

编程技术

编程语言java
正则匹配开源工具集  hutool
打印logback
字符串格式化lang3

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

keep-go-on

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值