空白字符匹配符 \s
\s 可以配置" "字符
非空白字符匹配符 \s
\S 可以配置非" "字符的
测试实例
被匹配字符串
private static final String test1 = "a1 2ad f31d-2 tt";
匹配公式1
匹配公式
String expression1 = "\\s";
匹配结果
注:匹配到三处空格
匹配公式2
匹配公式
String expression2 = "\\S";
匹配结果
注:匹配到了所有非空白的字符
测试代价【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;/*** @author keepgoon* @Description: TODO* @date 2019/10/211:34*/
public class RE4Test {private static Logger log = LoggerFactory.getLogger(RE3Test.class);private static final String test1 = "a1 2ad f31d-2 tt";public static void main(String[] args) {String expression1 = "\\s";String expression2 = "\\S";log.debug("\n===============匹配公式1 =======================");find(expression1,test1);log.debug("\n===============匹配公式2 =======================");find(expression2,test1);}private static String length(String printStr){return StringUtils.rightPad(printStr,10," ") + " : {}";}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);}
}
编程技术
编程语言 | java |
正则匹配 | 开源工具集 hutool |
打印 | logback |
字符串格式化 | lang3 |