更新时间:2021-05-06 11:51:57 来源:极悦 浏览1120次
SAML2报文内容如下,从中提取对应的attribute name和value.
<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.net</saml: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">amAdmin</saml:AttributeValue>
</saml:Attribute>
</saml:AttributeStatement>
show time
public static void main(String[] args) {
String content = "";
content = "<saml:AttributeStatement>";
content += " <saml:Attribute Name=\"mail\">";
content += " <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.net</saml:AttributeValue>";
content += " </saml:Attribute>";
content += " <saml:Attribute Name=\"cn\">";
content += " <saml:AttributeValue xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xs:string\">amAdmin</saml:AttributeValue>";
content += " </saml:Attribute>";
content += "</saml:AttributeStatement>";
String pattern = "\\<saml\\:Attribute Name=\\\"(?<scope>.*?)\\\"\\>[\\s\\S]*?\\<saml\\:AttributeValue[\\s\\S]*?\\>(?<value>.*?)\\<\\/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]*? 是用来匹配任意字符,表示在前后有界定的情况下(例如夹在中间的字符串),可以忽略那些不规则字符串的匹配。
//匹配整个ddl,将ddl分为表名,列sql部分,表注释
private static final Pattern DDL_PATTERN = Pattern.compile("\\s*create\\s+table\\s+(?<tableName>\\S+)[^\\(]*\\((?<columnsSQL>[\\s\\S]+)\\)[^\\)]+?(comment\\s*(=|on\\s+table)\\s*'(?<tableComment>.*?)'\\s*;?)?$", Pattern.CASE_INSENSITIVE);
//匹配列sql部分,分别解析每一列的列名 类型 和列注释
private static final Pattern COL_PATTERN = Pattern.compile("\\s*(?<fieldName>\\S+)\\s+(?<fieldType>\\w+)\\s*(?:\\([\\s\\d,]+\\))?((?!comment).)*(comment\\s*'(?<fieldComment>.*?)')?\\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 交费方式
以上就是极悦小编介绍的"Java正则表达式匹配字符串提取中间值"的内容,希望对大家有帮助,如有疑问,请在线咨询,有专业老师随时为您服务。
0基础 0学费 15天面授
Java就业班有基础 直达就业
业余时间 高薪转行
Java在职加薪班工作1~3年,加薪神器
工作3~5年,晋升架构
提交申请后,顾问老师会电话与您沟通安排学习