//需求:在分组1中匹配meta中author属性的值
//源串:
//<meta author="Zjmainstay" />
//another author="Zjmainstay too"
//预期:分组1得到Zjmainstay
//正则:
@Test
public void test1(){
String source="<meta author=\"Zjmainstay\" />\n" +
"another author=\"Zjmainstay too\"";
StringBuffer result=new StringBuffer();
Pattern pattern = Pattern.compile(
"<meta.*author=\"(.*)\"[^>]+>$",
Pattern.MULTILINE);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
System.out.println(matcher.group(1));
}
}
//(?<=[?&])(\w+)=(\w+)
@Test
public void test2(){
String source="https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=44004473_2_oem_dg&wd=libreOffice&fenlei=256&rsv_pq=b08801ed0018207f&rsv_t=2577CzDgz4Kf9tObFCys6YC4CE1XepzLb1nvO8zmcYnrEwZPQxwB2GPcE8qgfx74Xu0AOEESbrA&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_sug3=2&rsv_sug1=2&rsv_sug7=101&rsv_btype=i&prefixsug=libreOffice&rsp=3&rsv_sug4=5490";
HashMap<String, String> resultMap = new HashMap<>();
Pattern pattern = Pattern.compile(
"(?<=[?&])(\\w+)=(\\w+)",
Pattern.DOTALL);
Matcher matcher = pattern.matcher(source);
int index=1;
while(matcher.find()){
System.out.println(index+++"."+matcher.group(1)+"="+matcher.group(2));
resultMap.put(matcher.group(1),matcher.group(2));
}
}
@Test
public void test3(){
// String source="https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=44004473_2_oem_dg&wd=libreOffice&fenlei=256&rsv_pq=b08801ed0018207f&rsv_t=2577CzDgz4Kf9tObFCys6YC4CE1XepzLb1nvO8zmcYnrEwZPQxwB2GPcE8qgfx74Xu0AOEESbrA&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_sug3=2&rsv_sug1=2&rsv_sug7=101&rsv_btype=i&prefixsug=libreOffice&rsp=3&rsv_sug4=5490";
String source="https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=44004473_2_oem_dg&wd=libreOffice&fenl" +
"ei=256&rsv_pq=b08801ed0018207f&rsv_t_a_C=2577CzDgz4Kf9tObFCys6YC4CE1XepzLb1nvO8zmcYnrEwZPQxwB2GPcE8qgf" +
"x74Xu0AOEESbrA&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_sug3=2&rsv_sug1=2&rsv_sug7=101&rsv_btype=i&prefixsug=libreOffice&rsp=3&rsv_sug4=5490&a_b_c=123";
StringBuffer result=new StringBuffer();
Pattern pattern = Pattern.compile(
"(_\\w)",
Pattern.DOTALL);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
String valueBe=matcher.group(1);
String valueAf=valueBe.substring(1).toUpperCase();
matcher.appendReplacement(result,valueAf);
}
matcher.appendTail(result);
System.out.println(result.toString());
}
//需求:匹配每行字母个数是偶数个的数据,每行数据不为空,正则不能存在分组1
//源串:
//a
//ab
//abc
//abcd
//预期:
//匹配得到 ab 和 abcd,不包含分组1
@Test
public void test4(){
String source="a\n" +
"ab\n" +
"abc\n" +
"abcd\n" +
"alksad\n" +
"asdfa\n" +
"iiks\n";
Pattern pattern = Pattern.compile(
"^(?:[\\w][\\w])+$",
Pattern.MULTILINE
);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
//匹配无分组
System.out.println(source.substring(matcher.start(),matcher.end()));
}
}
//需求:匹配由 A/S/D/F 4个字母(区分大小写)组成的长度为3字符串
//源串:
//ABC
//ASD
//ADS
//ASF
//BBC
//A|S
//A|D
//ASDF
@Test
public void test5(){
String source="ABC\n" +
"ASD\n" +
"ADS\n" +
"ASF\n" +
"BBC\n" +
"A|S\n" +
"A|D\n" +
"ASDF";
Pattern pattern = Pattern.compile(
"^[ASDF][ASDF][ASDF]$",
Pattern.MULTILINE
);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
System.out.println(source.substring(matcher.start(),matcher.end()));
}
}
//sadf|affa&*^ds|asdf上课ds|2343|测试
//取第一个|跟最后一个|中间已|分隔的数据内容
@Test
public void test6(){
String source="sadf|affa&*^ds|asdf上课ds|2343|测试";
Pattern pattern = Pattern.compile(
"(?<=\\|)(.*?)(?=\\|)",
Pattern.DOTALL
);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
System.out.println(matcher.group(1));
}
}
//需求:匹配每行数据中以.jpg/.jpeg/.png/.gif结尾的图片名称(含后缀)
//源串:
//image.jpg
//image.jpeg
//image.png
//image.gif
//not_image.txt
//not_image.doc
//not_image.xls
//not_image.ppt
//预期:匹配 image.jpg/image.jpeg/image.png/image.gif 4个结果
@Test
public void test7(){
String source="image.jpg\n" +
"image.jpeg\n" +
"image.png\n" +
"image.gif\n" +
"not_image.txt\n" +
"not_image.doc\n" +
"not_image.xls\n" +
"not_image.ppt";
Pattern pattern = Pattern.compile(
"^([\\w\\W]+?)(?<=\\.jpg|\\.jpeg|\\.png|\\.gif)$",
Pattern.MULTILINE
);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
System.out.println(matcher.group(1));
}
}
//匹配连续相同3次的数字
//源串:
//111
//121
//112
//222
//预期:匹配 111/222 两组数据
@Test
public void test8(){
String source="111\n" +
"121\n" +
"112\n" +
"222";
Pattern pattern = Pattern.compile(
"^(\\d)\\1\\1$",
Pattern.MULTILINE
);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
System.out.println(source.substring(matcher.start(),matcher.end()));
}
}
//分别使用单行模式和普通模式匹配id="author"的div中数据,div标签不在同一行
//源串:
//<div id="author">
//Zjmainstay
//</div>
//预期:Zjmainstay
@Test
public void test9(){
String source="<div id=\"author\">\n" +
"Zjmainstay\n" +
"</div>\n" +
"<div id=\"author1\">\n" +
"Zjmainstay1\n" +
"</div>";
Pattern pattern = Pattern.compile(
"(?<=id=\"author\"\\s{0,100}>\\s{0,100})\\w+(?=\\s*<\\/div>)"
);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
System.out.println(source.substring(matcher.start(),matcher.end()));
}
}
//需求:匹配每行中包含“作者”或者“读者”的数据
//源串:
//本文的作者是Zjmainstay
//本文有很多读者
//读者可以是任何一个地方的人
//这里的任何一个地方说明读者也能在国外
//什么乱七八糟的推理
//你不匹配我,凭什么要我推荐你的博客 www.zjmainstay.cn
//预期:匹配
//本文的作者是Zjmainstay
//本文有很多读者
//读者可以是任何一个地方的人
//这里的任何一个地方说明读者也能在国外
@Test
public void test10(){
String source="本文的作者是Zjmainstay\n" +
"本文有很多读者\n" +
"读者可以是任何一个地方的人\n" +
"这里的任何一个地方说明读者也能在国外\n" +
"什么乱七八糟的推理\n" +
"你不匹配我,凭什么要我推荐你的博客 www.zjmainstay.cn";
Pattern pattern = Pattern.compile(
"^.*(作者|读者).*$",
Pattern.MULTILINE
);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
System.out.println(source.substring(matcher.start(),matcher.end()));
}
}
//匹配多种或条件的数据,有特殊限制(不使用环视)
//需求:匹配每行中“读者”在开头或结尾的数据
//源串:
//本文作者是Zjmainstay,有很多读者
//读者可以是任何一个地方的人
//这里的任何一个地方说明读者也能在国外
//预期:匹配
//本文作者是Zjmainstay,有很多读者
//读者可以是任何一个地方的人
@Test
public void test11(){
String source="本文作者是Zjmainstay,有很多读者\n" +
"读者可以是任何一个地方的人\n" +
"这里的任何一个地方说明读者也能在国外";
Pattern pattern = Pattern.compile(
"(^读者.*$|^.*读者$)",
Pattern.MULTILINE
);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
System.out.println(matcher.group(1));
}
}
//匹配多种或条件的数据,有特殊限制(使用环视)
//需求:匹配每行中“读者”在开头或结尾的数据
//源串:
//本文作者是Zjmainstay,有很多读者
//读者可以是任何一个地方的人
//这里的任何一个地方说明读者也能在国外
//预期:匹配
//本文作者是Zjmainstay,有很多读者
//读者可以是任何一个地方的人
@Test
public void test12(){
String source="本文作者是Zjmainstay,有很多读者\n" +
"读者可以是任何一个地方的人\n" +
"这里的任何一个地方说明读者也能在国外";
Pattern pattern = Pattern.compile(
"^((?=.*^读者).*$|.*(?<=读者$))$",
Pattern.MULTILINE
);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
System.out.println(matcher.group(1));
}
}
//需求:校验密码必须包含字母、数字和特殊字符,6-16位,假定特殊字符为 -_= 三个字符
//源串:
//12345
//123456
//1234561234561234
//12345612345612345
//a1234
//a12345
//-1234
//-12345
//a-123
//a-1234
//a-1234a-1234a-12
//a-1234a-1234a-1234
//aaaaa
//aaaaaa
//-_=-_
//-_=-_=
//预期:匹配
//a-1234
//a-1234a-1234a-12
@Test
public void test13(){
String source="12345\n" +
"123456\n" +
"1234561234561234\n" +
"12345612345612345\n" +
"a1234\n" +
"a12345\n" +
"-1234\n" +
"-12345\n" +
"a-123\n" +
"a-1234\n" +
"a-1234a-1234a-12\n" +
"a-1234a-1234a-1234\n" +
"aaaaa\n" +
"aaaaaa\n" +
"-_=-_\n" +
"-_=-_=";
Pattern pattern = Pattern.compile(
"(?=.*[a-zA-Z])(?=.*\\d)(?=.*[-_=])^.{6,16}$",
Pattern.MULTILINE
);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
System.out.println(source.substring(matcher.start(),matcher.end()));
}
}
//特殊限制(环视否定)
//(8.1)使用\d{1,3}匹配1-999的数据,不能以0开头
//需求:使用\d{1,3}匹配每行中1-999的数据,不能以0开头
//源串:
//1
//10
//100
//999
//1000
//01
//001
//预期:匹配
//1
//10
//100
//999
@Test
public void test14(){
String source="1\n" +
"10\n" +
"100\n" +
"999\n" +
"1000\n" +
"01\n" +
"001";
Pattern pattern = Pattern.compile(
"(?!.*^0)^\\d{1,3}$",
Pattern.MULTILINE
);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
System.out.println(source.substring(matcher.start(),matcher.end()));
}
}
//匹配除了span标签外的所有标签
//需求:匹配除了<span>内容</span>标签外的所有<tagName>内容</tagName>格式标签
//源串:
//<div>匹配我</div>
//<span>不匹配我</span>
//<p>匹配我</p>
//<i>匹配我</i>
//预期:匹配
//<div>匹配我</div>
//<p>匹配我</p>
//<i>匹配我</i>
@Test
public void test15(){
String source="<div>匹配我</div>\n" +
"<span>不匹配我</span>\n" +
"<p>匹配我</p>\n" +
"<i>匹配我</i>\n";
Pattern pattern = Pattern.compile(
"^<(?!.*span)(.*)$",
Pattern.MULTILINE
);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
System.out.println(matcher.group(1));
}
}
//需求:给源串每个链接加上http://www.zjmainstay.cn前缀
//源串:
//<a id="link-1" href="/regexp-one">正则文章合集(All In One)</a>
//<a id="link-2" href="/my-regexp">正则入门教程</a>
//<a id="link-3" href="/deep-regexp">正则高级教程</a>
//<a id="link-4" href="/regexp-lookaround">正则环视详解</a>
//<a id="link-5" href="/php-curl">PHP cURL应用</a>
//预期:替换得到
//<a id="link-1" href="http://www.zjmainstay.cn/regexp-one">正则文章合集(All In One)</a>
//<a id="link-2" href="http://www.zjmainstay.cn/my-regexp">正则入门教程</a>
//<a id="link-3" href="http://www.zjmainstay.cn/deep-regexp">正则高级教程</a>
//<a id="link-4" href="http://www.zjmainstay.cn/regexp-lookaround">正则环视详解</a>
//<a id="link-5" href="http://www.zjmainstay.cn/php-curl">PHP cURL应用</a>
@Test
public void test16(){
String source= "<a id=\"link-1\" href=\"http://www.zjmainstay.cn/regexp-one\">正则文章合集(All In One)</a>\n" +
"<a id=\"link-2\" href=\"http://www.zjmainstay.cn/my-regexp\">正则入门教程</a>\n" +
"<a id=\"link-3\" href=\"http://www.zjmainstay.cn/deep-regexp\">正则高级教程</a>\n" +
"<a id=\"link-4\" href=\"http://www.zjmainstay.cn/regexp-lookaround\">正则环视详解</a>\n" +
"<a id=\"link-5\" href=\"http://www.zjmainstay.cn/php-curl\">PHP cURL应用</a>";
StringBuffer result=new StringBuffer();
Pattern pattern = Pattern.compile(
"(?<=href=\")(\\/)",
Pattern.MULTILINE
);
Matcher matcher = pattern.matcher(source);
String replaceStr="http://www.zjmainstay.cn/";
while(matcher.find()){
matcher.appendReplacement(result,replaceStr);
}
matcher.appendTail(result);
System.out.println(result);
}
//需求:将每行特定格式数据格式化为SQL语句
//源串:
//1 2017-04-11 Zjmainstay
//2 2017-04-12 Nobody
//3 2017-04-13 Somebody
//预期:替换得到
//INSERT INTO table_log(`id`, `created_at`, `author`) values('1', '2017-04-11', 'Zjmainstay');
//INSERT INTO table_log(`id`, `created_at`, `author`) values('2', '2017-04-12', 'Nobody');
//INSERT INTO table_log(`id`, `created_at`, `author`) values('3', '2017-04-13', 'Somebody');
@Test
public void test17(){
String source="1 2017-04-11 Zjmainstay\n" +
"2 2017-04-12 Nobody\n" +
"3 2017-04-13 Somebody";
Matcher matcher = Pattern.compile(
"^([\\w])\\s([\\w-]+)\\s([\\w]+)",
Pattern.MULTILINE
).matcher(source);
String format="INSERT INTO table_log(`id`, `created_at`, `author`) values('$1', '$2', '$3');";
while(matcher.find()){
System.out.println(matcher.replaceAll(format));
}
}
//需求:匹配html标签的属性值,属性值可以由双引号、单引号、无单双引号定界
//源串:
//<div id="I'm Zjmainstay" class="name" data-year=2017 age='27'>
//预期:分组匹配
//I'm Zjmainstay
//author
//2017
//27
@Test
public void test18(){
String source="<div id=\"I'm Zjmainstay\" class=\"name\" data-year=2017 age='27'>";
Matcher matcher = Pattern.compile(
"(?<==)([\"']?)(.*?)\\1[\\s>]",
Pattern.DOTALL
).matcher(source);
while(matcher.find()){
System.out.println(matcher.group(2));
}
}
//需求:匹配0.00-100.00的数值,可以有0-2位小数,不能以小数点结尾,不能以2个以上的0开头
//思路:(100|10-99|0-9) + 0-2小数位 + 排除小数点结尾、2个以上0开头的情况
//源串:
//0
//1
//0.0
//0.00
//9.00
//18.00
//27.0
//36.00
//45.00
//54.00
//63.00
//72.00
//81.00
//90.00
//99.99
//100.00
//0.
//001
//100.01
//100.001
//101
//预期:匹配0.00~100.00
@Test
public void test19(){
String source="0\n" +
"1\n" +
"0.0\n" +
"0.00\n" +
"9.00\n" +
"18.00\n" +
"27.0\n" +
"36.00\n" +
"35.\n" +
"45.00\n" +
"54.00\n" +
"63.00\n" +
"72.00\n" +
"81.00\n" +
"90.00\n" +
"99.99\n" +
"100.00\n" +
"0.\n" +
"001\n" +
"100.01\n" +
"100.001\n" +
"101";
Matcher matcher = Pattern.compile(
"(?!.*^00)^(([0-9]|(?:[1-9]\\d))(?:\\.\\d{1,2})?|100.00)(?!<\\.$)$",
Pattern.MULTILINE
).matcher(source);
while(matcher.find()){
System.out.println(source.substring(matcher.start(),matcher.end()));
}
}
//匹配链接中的文件名
//需求:利用贪婪模式,分组1得到每行链接中的文件名
//源串:
//http://localhost.com/a/b/c/d/file1.txt
//https://localhost.com/a/b/file2long.jpg
//预期:分组0匹配行数据,分组1匹配文件名
//file1.txt
//file2long.jpg
@Test
public void test20(){
String source="http://localhost.com/a/b/c/d/file1.txt\n" +
"https://localhost.com/a/b/file2long.jpg";
Matcher matcher = Pattern.compile(
"\\/([\\w\\.]+)$",
Pattern.MULTILINE
).matcher(source);
while(matcher.find()){
System.out.println(matcher.group(1));
}
}
//限定字符贪婪优化匹配性能
//需求:匹配div id="author"的标签内容
//源串:
//<div id="author" class="author-text something-useless">Zjmainstay</div>
//预期:利用贪婪模式去掉div中的噪点(无关数据),分组1匹配到Zjmainstay
//正则:
@Test
public void test21(){
String source="<div id=\"author\" class=\"author-text something-useless\">Zjmainstay</div>";
Matcher matcher = Pattern.compile(
"<div[^>]+>(.*)<\\/div>",
Pattern.DOTALL
).matcher(source);
while(matcher.find()){
System.out.println(matcher.group(1));
}
}
//需求:匹配p标签内容
//源串:
//<p>内容1</p><p>内容2</p>
//预期:
//在分组1中匹配到内容1和内容2
@Test
public void test22(){
String source="<p>内容1</p><p>内容2</p>";
Matcher matcher = Pattern.compile(
"<p>(.*?)<\\/p>",
Pattern.DOTALL
).matcher(source);
while(matcher.find()){
System.out.println(matcher.group(1));
}
}
//需求:在分组1中匹配css或script的链接
//源串:
//<script src="main.min.js" type="text/javascript"></script>
//<link rel="stylesheet" type="text/css" href="main.css">
//预期:
//main.min.js
//main.css
@Test
public void test23(){
String source="<script src=\"main.min.js\" type=\"text/javascript\"></script>\n" +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"main.css\">";
Matcher matcher = Pattern.compile(
"(?:src=\"|href=\")([\\w\\W]+?)(?=\")",
Pattern.MULTILINE
).matcher(source);
while(matcher.find()){
System.out.println(matcher.group(1));
}
}
//需求:表达式格式固定,提取其中的数值
//源串:
//(20+170)-5*1/5=?
//预期:
//A:20
//B:170
//C:5
//D:1
//E:5
//F:?
@Test
public void test24(){
String source="(20+170)-5*1/5=?";
Matcher matcher = Pattern.compile(
"\\((\\d+)\\+(\\d+)\\)\\-(\\d+)\\*(\\d+)\\/(\\d+)=(\\?)",
Pattern.DOTALL
).matcher(source);
while(matcher.find()){
System.out.println("A:"+matcher.group(1));
System.out.println("B:"+matcher.group(2));
System.out.println("C:"+matcher.group(3));
System.out.println("D:"+matcher.group(4));
System.out.println("E:"+matcher.group(5));
System.out.println("F:"+matcher.group(6));
}
}
//需求:在不对/转义的情况下匹配p标签内容
//源串:
//<p>内容1</p><p>内容2</p>
//预期:
//在分组1中匹配到内容1和内容2
@Test
public void test25(){
String source="<p>内容1</p><p>内容2</p>";
Matcher matcher = Pattern.compile(
"<p>(.*?)(?=<)",
Pattern.DOTALL
).matcher(source);
while(matcher.find()){
System.out.println(matcher.group(1));
}
}
//需求:匹配内容为数字的div
//源串:
//<div class="aaa bbb">ABC</div><div class="bbb ccc">123</div>
//预期:
//<div>123</div>
//错误正则:/<div.*?>\d+<\/div>/
@Test
public void test26(){
String source="<div class=\"aaa bbb\">ABC</div><div class=\"bbb ccc\">123</div>";
Matcher matcher = Pattern.compile(
"<div[^>]*>\\d+<\\/div>",
Pattern.DOTALL
).matcher(source);
while(matcher.find()){
System.out.println(source.substring(matcher.start(),matcher.end()));
}
}
//需求:匹配不包含某个单词或词语的内容
//源串:
//http://www.zjmainstay.cn
//http://www.baidu.com
//http://www.qq.com
//预期:
//http://www.zjmainstay.cn
//http://www.qq.com
@Test
public void test27(){
String source="http://www.zjmainstay.cn\n" +
"http://www.baidu.com\n" +
"http://www.qq.com";
Matcher matcher = Pattern.compile(
"^(?!(?:.*baidu.*))(.*)",
Pattern.MULTILINE
).matcher(source);
while(matcher.find()){
System.out.println(source.substring(matcher.start(),matcher.end()));
}
}
//需求:匹配不包含某个单词或词语的内容
//源串:
//A("Excalibur", "誓约胜利之剑", LONG_SWORD, (SPFX_NOGEN | SPFX_RESTR | SPFX_SEEK | SPFX_DEFN | SPFX_INTEL | SPFX_SEARCH),
// 0, 0, PHYS(5, 10), DRLI(0, 0), NO_CARY, 0, A_LAWFUL, PM_KNIGHT, NON_PM, 4000L, NO_COLOR);
///*
// * Stormbringer only has a 2 because it can drain a level,
// * providing 8 more.
// */
//A("Stormbringer", "兴风者", RUNESWORD,
// (SPFX_RESTR | SPFX_ATTK | SPFX_DEFN | SPFX_INTEL | SPFX_DRLI), 0, 0,
// DRLI(5, 2), DRLI(0, 0), NO_CARY, 0, A_CHAOTIC, NON_PM, NON_PM, 8000L,
// NO_COLOR);
///*
// * Mjollnir will return to the hand of the wielder when thrown
// * if the wielder is a Valkyrie wearing Gauntlets of Power.
// */
//A("Mjollnir", "雷神之锤", WAR_HAMMER, /* Mjo:llnir */
// (SPFX_RESTR | SPFX_ATTK), 0, 0, ELEC(5, 24), NO_DFNS, NO_CARY, 0,
// A_NEUTRAL, PM_VALKYRIE, NON_PM, 4000L, NO_COLOR);
//A("Cleaver", "撕裂者", BATTLE_AXE, SPFX_RESTR, 0, 0, PHYS(3, 6), NO_DFNS, NO_CARY, 0, A_NEUTRAL, PM_BARBARIAN, NON_PM, 1500L, NO_COLOR);
///*
// * Grimtooth glows in warning when elves are present, but its
// * damage bonus applies to all targets rather than just elves
// * (handled as special case in spec_dbon()).
// */
//A("Grimtooth", "邪兽之牙", ORCISH_DAGGER, (SPFX_RESTR | SPFX_WARN | SPFX_DFLAG2),
// 0, M2_ELF, PHYS(2, 6), NO_DFNS,
// NO_CARY, 0, A_CHAOTIC, NON_PM, PM_ORC, 300L, CLR_RED);
///*
// * Orcrist and Sting have same alignment as elves.
// *
// * The combination of SPFX_WARN+SPFX_DFLAG2+M2_value will trigger
// * EWarn_of_mon for all monsters that have the M2_value flag.
// * Sting and Orcrist will warn of M2_ORC monsters.
// */
//A("Orcrist", "杀兽剑", ELVEN_BROADSWORD, (SPFX_WARN | SPFX_DFLAG2), 0, M2_ORC, PHYS(5, 0),
// NO_DFNS, NO_CARY, 0, A_CHAOTIC, NON_PM, PM_ELF, 2000L, CLR_BRIGHT_BLUE); /* bright blue is actually light blue */
//预期:
//Excalibur=誓约胜利之剑
//Stormbringer=兴风者
//Mjollnir=雷神之锤
//Cleaver=撕裂者
//Grimtooth=邪兽之牙
//Orcrist=杀兽剑
//查找:
//替换:
@Test
public void test28(){
String source="A(\"Excalibur\", \"誓约胜利之剑\", LONG_SWORD, (SPFX_NOGEN | SPFX_RESTR | SPFX_SEEK | SPFX_DEFN | SPFX_INTEL | SPFX_SEARCH), \n" +
" 0, 0, PHYS(5, 10), DRLI(0, 0), NO_CARY, 0, A_LAWFUL, PM_KNIGHT, NON_PM, 4000L, NO_COLOR);\n" +
"/*\n" +
" * Stormbringer only has a 2 because it can drain a level,\n" +
" * providing 8 more.\n" +
" */\n" +
"A(\"Stormbringer\", \"兴风者\", RUNESWORD,\n" +
" (SPFX_RESTR | SPFX_ATTK | SPFX_DEFN | SPFX_INTEL | SPFX_DRLI), 0, 0,\n" +
" DRLI(5, 2), DRLI(0, 0), NO_CARY, 0, A_CHAOTIC, NON_PM, NON_PM, 8000L,\n" +
" NO_COLOR);\n" +
"/*\n" +
" * Mjollnir will return to the hand of the wielder when thrown\n" +
" * if the wielder is a Valkyrie wearing Gauntlets of Power.\n" +
" */\n" +
"A(\"Mjollnir\", \"雷神之锤\", WAR_HAMMER, /* Mjo:llnir */\n" +
" (SPFX_RESTR | SPFX_ATTK), 0, 0, ELEC(5, 24), NO_DFNS, NO_CARY, 0,\n" +
" A_NEUTRAL, PM_VALKYRIE, NON_PM, 4000L, NO_COLOR);\n" +
"A(\"Cleaver\", \"撕裂者\", BATTLE_AXE, SPFX_RESTR, 0, 0, PHYS(3, 6), NO_DFNS, NO_CARY, 0, A_NEUTRAL, PM_BARBARIAN, NON_PM, 1500L, NO_COLOR);\n" +
"/*\n" +
" * Grimtooth glows in warning when elves are present, but its\n" +
" * damage bonus applies to all targets rather than just elves\n" +
" * (handled as special case in spec_dbon()).\n" +
" */\n" +
"A(\"Grimtooth\", \"邪兽之牙\", ORCISH_DAGGER, (SPFX_RESTR | SPFX_WARN | SPFX_DFLAG2),\n" +
" 0, M2_ELF, PHYS(2, 6), NO_DFNS,\n" +
" NO_CARY, 0, A_CHAOTIC, NON_PM, PM_ORC, 300L, CLR_RED);\n" +
"/*\n" +
" * Orcrist and Sting have same alignment as elves.\n" +
" *\n" +
" * The combination of SPFX_WARN+SPFX_DFLAG2+M2_value will trigger\n" +
" * EWarn_of_mon for all monsters that have the M2_value flag.\n" +
" * Sting and Orcrist will warn of M2_ORC monsters.\n" +
" */\n" +
"A(\"Orcrist\", \"杀兽剑\", ELVEN_BROADSWORD, (SPFX_WARN | SPFX_DFLAG2), 0, M2_ORC, PHYS(5, 0), \n" +
" NO_DFNS, NO_CARY, 0, A_CHAOTIC, NON_PM, PM_ELF, 2000L, CLR_BRIGHT_BLUE); /* bright blue is actually light blue */";
Matcher matcher = Pattern.compile(
"(?<=A\\(\")([\\w\\W]+?)\"(?:[^\"]+)\"([\\w\\W]+?)(?=\")",
Pattern.MULTILINE
).matcher(source);
while(matcher.find()){
System.out.println(matcher.group(1)+"="+matcher.group(2));
}
}
//需求:循环提取每行数据的分支部分和固定部分,格式化为特定格式
//源串:
//BBB|CCC|DDD=AAA
//FFF|GGG|HHH|III|JJJ|KKK=EEE
//预期:
//BBB=AAA
//CCC=AAA
//DDD=AAA
//FFF=EEE
//GGG=EEE
//HHH=EEE
//III=EEE
//JJJ=EEE
//KKK=EEE
//查找:
//替换:
@Test
public void test29(){
String source="BBB|CCC|DDD=AAA\n" +
"FFF|GGG|HHH|III|JJJ|KKK=EEE";
String [] splitStr=source.split("\\n");
for(String part:splitStr){
ArrayList<String> keyList = new ArrayList<>();
String value="";
Matcher matcher = Pattern.compile(
"([\\w]+?)(?:(?:[\\|=]))",
Pattern.DOTALL
).matcher(part);
while(matcher.find()){
keyList.add(matcher.group(1));
}
matcher = Pattern.compile(
"(?<==)(\\w+)",
Pattern.DOTALL
).matcher(part);
while(matcher.find()){
value=matcher.group(1);
}
for(String key:keyList){
System.out.println(key+"="+value);
}
}
}
常用正则表达式JAVA练习笔记
最新推荐文章于 2024-07-21 21:00:39 发布