java正则小例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
*
* attributeName->mail,attributeValue->zhengkai.blog.csdn.net
attributeName->cn,attributeValue->amAdmin
*/
private static void test() {
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(content);
while (matcher.find()){
String attributeName = matcher.group("scope");
String attributeValue = matcher.group("value");
System.out.println("attributeName->"+attributeName +",attributeValue->"+attributeValue );
}
}

private static void prehand() {
Pattern patten = Pattern.compile("\\w*\\.*\\w+\\s*\\=\\s*\\'\\$\\{b00\\}\\'");
String resultStr = "a0201b='${b00}' and asdasd.a0201b='${b00}' and asda0201b='${b00}'";
Matcher matcher = patten.matcher(resultStr);
while(matcher.find()) {
String[] split = "哈哈,giegie".split(",");
String attributeName = matcher.group();
System.out.println(attributeName);
String key = attributeName.replace("'${b00}'", "").replace("=", "").trim();
String replace = Stream.of(split).map(e->"'"+e+"'").collect(Collectors.joining(",", key+" in (", ")"));
resultStr = resultStr.replace(attributeName, replace);
}
System.out.println(resultStr);

}