Java正则表达式匹配字符串并提取中间的值

前言

有时候正则表达式不只是匹配一下什么数字/邮箱/身份证/日期等等,还需要匹配某一段文字,并按照既定格式提取其中的某些值.

场景一:提取SAML2报文

SAML2报文内容如下,从中提取对应的attribute namevalue.

<saml:AttributeStatement>
	<saml:Attribute Name="mail">
		<saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">zhengkai.blog.csdn.netsaml:AttributeValue>
	saml:Attribute>
	<saml:Attribute Name="cn">
		<saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">amAdminsaml:AttributeValue>
	saml:Attribute>
saml:AttributeStatement>

show time

public static void main(String[] args) {
		String content = "";
		content = "";
		content += "	";
		content += "		zhengkai.blog.csdn.net";
		content += "	";
		content += "	";
		content += "		amAdmin";
		content += "	";
		content += "";
		String pattern = "\\.*?)\\\"\\>[\\s\\S]*?\\(?.*?)\\<\\/saml\\:AttributeValue\\>[\\s\\S]*?\\<\\/saml\\:Attribute\\>";
		Pattern r = Pattern.compile(pattern);
		Matcher matcher = r.matcher(saml2attributeString);
		while (matcher.find()){
			String attributeName = matcher.group("scope");
			String attributeValue = matcher.group("value");
			System.out.println("attributeName->"+attributeName +",attributeValue->"+attributeValue );
		}
	}

控制台输出

$ attributeName->mail,attributeValue->zhengkai.blog.csdn.net
$ vattributeName->cn,attributeValue->amAdmin

解析

\\<saml\\:Attribute Name=\\\"(?<scope>.*?)\\\"\\>[\\s\\S]*?\\<saml\\:AttributeValue[\\s\\S]*?\\>(?<value>.*?)\\<\\/saml\\:AttributeValue\\>[\\s\\S]*?\\<\\/saml\\:Attribute\\>
  1. (?.*?)是用于标识scope的方式,表示夹在中间的字符串都叫scope,可以通过matcher.group("scope")提取。
  2. 所有正则的符号都需要转义,所以你会看到很多\\<或者换\\"之类的\\
  3. [\\s\\S]*? 是用来匹配任意字符,表示在前后有界定的情况下(例如夹在中间的字符串),可以忽略那些不规则字符串的匹配。

场景2:提取sql中的表名和字段

来自github网友@ydqSpringBootCodeGenerator贡献的正则表达式,非常不错,值得深刻学习.

    //匹配整个ddl,将ddl分为表名,列sql部分,表注释
    private static final Pattern DDL_PATTERN = Pattern.compile("\\s*create\\s+table\\s+(?\\S+)[^\\(]*\\((?[\\s\\S]+)\\)[^\\)]+?(comment\\s*(=|on\\s+table)\\s*'(?.*?)'\\s*;?)?$", Pattern.CASE_INSENSITIVE);
    //匹配列sql部分,分别解析每一列的列名 类型 和列注释
    private static final Pattern COL_PATTERN = Pattern.compile("\\s*(?\\S+)\\s+(?\\w+)\\s*(?:\\([\\s\\d,]+\\))?((?!comment).)*(comment\\s*'(?.*?)')?\\s*(,|$)", Pattern.CASE_INSENSITIVE);

    public static void parse(String sql){
        Matcher matcher = DDL_PATTERN.matcher(sql);
        if (matcher.find()){
            String tableName = matcher.group("tableName");
            String tableComment = matcher.group("tableComment");
            System.out.println(tableName + "\t\t" + tableComment);
            System.out.println("==========");
            String columnsSQL = matcher.group("columnsSQL");
            if (columnsSQL != null && columnsSQL.length() > 0){
                Matcher colMatcher = COL_PATTERN.matcher(columnsSQL);
                while (colMatcher.find()){
                    String fieldName = colMatcher.group("fieldName");
                    String fieldType = colMatcher.group("fieldType");
                    String fieldComment = colMatcher.group("fieldComment");
                    if (!"key".equalsIgnoreCase(fieldType)){
                        System.out.println(fieldName + "\t\t" + fieldType + "\t\t" + fieldComment);
                    }
                }
            }
        }
    }

    public static void main(String[] args){
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        parse("CREATE TABLE `userinfo` (\n" +
                "  `user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',\n" +
                "  `username` varchar(255) NOT NULL COMMENT '用户名',\n" +
                "  `addtime` datetime NOT NULL COMMENT '创建时间',\n" +
                "  PRIMARY KEY (`user_id`)\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户信息'");
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        parse("CREATE TABLE `USER` (\n" +
                "`ID` varchar(32) PRIMARY KEY COMMENT '主键',\n" +
                "`password` varchar(32) NOT NULL COMMENT '密码',\n" +
                "`username` varchar(32) NOT NULL COMMENT '用户'\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        parse("CREATE TABLE `tb_amount` (\n" +
                "`ID` int(10) NOT NULL AUTO_INCREMENT,\n" +
                "`PRODUCT_CODE` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '产品代码',\n" +
                "`GENDER` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '性别(male,female)',\n" +
                "`MIN_INSURED_AGE` int(3) NULL DEFAULT NULL COMMENT '最小投保年龄',\n" +
                "`MAX_INSURED_AGE` int(3) NULL DEFAULT NULL COMMENT '最大投保年龄',\n" +
                "`AMOUNT` double(10, 2) NULL DEFAULT NULL COMMENT '基本保额',\n" +
                "`PREMIUM_RATE` double(10, 2) NULL DEFAULT NULL COMMENT '基本保费',\n" +
                "`YEAR_NUM` int(3) NULL DEFAULT NULL COMMENT '缴费年限',\n" +
                "`PREMIUM_TYPE` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '费率类型',\n" +
                "`INSURANCE_PERIOD` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '保险期间(30年,60年)',\n" +
                "`INSURANCE_PERIOD_TYPE` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '保险期间类型(如定期年0,定期岁1,终身2,以后终身对应值:200)',\n" +
                "`PAY_MODE` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '交费方式',\n" +
                "PRIMARY KEY (`ID`) USING BTREE\n" +
                ") ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;");
    }

控制台输出内容:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
`userinfo`		用户信息
==========
`user_id`		int		用户ID
`username`		varchar		用户名
`addtime`		datetime		创建时间
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
`USER`		null
==========
`ID`		varchar		主键
`password`		varchar		密码
`username`		varchar		用户
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
`tb_amount`		null
==========
`ID`		int		null
`PRODUCT_CODE`		varchar		产品代码
`GENDER`		varchar		性别(male,female)
`MIN_INSURED_AGE`		int		最小投保年龄
`MAX_INSURED_AGE`		int		最大投保年龄
`AMOUNT`		double		基本保额
`PREMIUM_RATE`		double		基本保费
`YEAR_NUM`		int		缴费年限
`PREMIUM_TYPE`		varchar		费率类型
`INSURANCE_PERIOD`		varchar		保险期间(30年,60年)
`INSURANCE_PERIOD_TYPE`		varchar		保险期间类型(如定期年0,定期岁1,终身2,以后终身对应值:200)
`PAY_MODE`		varchar		交费方式

你可能感兴趣的:(杂谈)