多语言展示
当前在线:927今日阅读:39今日分享:10

正则表达式30分钟入门系列之10

正则表达式也是一种语言,在解析字符串领域独领风骚。本文分享关于正则表达式中最常用的关键字()及相关的分组的用法
工具/原料
1

IntelliJ IDEA

2

Java

3

Regular expressions

方法/步骤
1

():由关键字'()'括起来的在正则表达式中代表”分组“或”子表达式“的含义在正则表达式匹配的层面,这个”分组“或”子表达式“没有特殊的用途但在编程语言中”分组“就有特殊的用途,最常见的场景,拼sql时,有些where条件是不确定的,需要根据实际情况进行替换先来看看测试的脚手架代码:Code:package chapter4;import java.util.Arrays;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Created by MyWorld on 2016/3/27. */public class RegexGroupDemo {    public static void main(String[] args) {        String expectedSql = 'select id,name from user where deptId=100 and name='John' and age=30';        String sql = 'select id,name from user where deptId={0} and name='{2}' and age={1}';        String regex = '\\{(\\d)\\}';        List input = Arrays.asList('100', '30', 'John');        Pattern pattern = Pattern.compile(regex);        Matcher matcher = pattern.matcher(sql);        while (matcher.find()) {            System.out.println(matcher.group(0) + '   group 1:' + matcher.group(1));//            sql = sql.replace(matcher.group(0), input.get(Integer.valueOf(matcher.group(1))));        }//        System.out.println(sql);//        assertThat(sql, is(expectedSql));    }}

2

执行下看看结果:Output:{0}   group 1:0{2}   group 1:2{1}   group 1:1

3

先介绍上面使用的API:Java Matcher.find()public boolean find()        Attempts to find the next subsequence of the input sequence that matches the pattern.    This method starts at the beginning of this matcher's region, or,    if a previous invocation of the method was successful and the matcher has not since been reset,    at the first character not matched by the previous match.    If the match succeeds then more information can be obtained via the start, end, and group methods.    Returns:            true if, and only if, a subsequence of the input sequence matches this matcher's pattern解析下上面的结果:matcher.group(0):表示整个表达式匹配的结果。示例中的正则表达式'\\{(\\d)\\}'能匹配到'{0}'、'{1}'、'{2}'group(int number):number是不为0的数字,则表示获取的是分组或子表达式number=1时,是第一个分组,从左向右数,第1个如果分组有嵌套时,先从最外层的开始算,譬如'\\w(=\\{(\\d))\\}'使用find()匹配'select id,name from user where deptId={0} and name='{2}' and age={1}'时,得到的结果是:d={0}   group 1:={0   group 2:0e={1}   group 1:={1   group 2:1

4

把上面的代码改造成实现预期的业务,把sql中相关变量替换成相关的业务数字更改代码Code:while (matcher.find()) {    System.out.println(matcher.group(0) + '   group 1:' + matcher.group(1));    sql = sql.replace(matcher.group(0), input.get(Integer.valueOf(matcher.group(1))));}System.out.println(sql);assertThat(sql, is(expectedSql));

5

执行下看看结果Output:{0}   group 1:0{2}   group 1:2{1}   group 1:1select id,name from user where deptId=100 and name='John' and age=30与预期一致。对JDBC有深入了解的tx,会觉得上面的写法比较low,因为PreparedStatement有预编译,没必要在执行sql前替换上面说的很对不过这两种处理方式,各有利弊了本文中使用的方式,可以不用考虑待替换sql中各个待替换表达式的位置和在sql中的顺序,只要id一样就OK了是不是很省事并且对于只执行一次的sql,预编译也没有太大优势哦如果此sql频繁执行且变量每次都不同,建议使用预编译

6

说下本系列最后一个语法:注释(?#comment)正则表达式如果涉及的逻辑比较多,则可读性会下降这时候注释就有用了把上面的正则表达式加个注释Code:String regex = '\\{(\\d)\\}(?#匹配ID标识)';java对这咱内嵌式注释不支持,此处就不演示了

7

正则表达式30分钟入门系列小结一下:这个系统已经介绍过两类正则表达式关键词表示某类字符的表示数量的,即某类字符出现多少次分组的基本上掌握上面的正则表达式的关键词,常用的场景就可以应对了OK此系统结束如果在使用过程遇到问题,可以找我探讨哦3q==================It's over==================

推荐信息