优化 java 模式匹配函数以不同的顺序打印,可能的链表操作
refine java pattern matching function to print in a different order, possible linkedlist operation
此刻我的代码如下所示。它非常简单,它只是读入一个数据文件并抓取所有有趣的位并打印出来。麻烦的是,它打印出来的方式不对,顺序不对。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class text_processing
{
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException
{
String text;
BufferedReader br = new BufferedReader(new FileReader("/home/matthias/Workbench/SUTD/1_February/brute_force/items.csv"));
while ((text = br.readLine()) != null)
{
//the main character
Pattern pat_0 = Pattern.compile( "『(.*?)』" );
Matcher mat_0 = pat_0.matcher( text );
if( mat_0.find() )
{
System.out.println( mat_0.group(1) );
}
//the pin yin
Pattern pat_1 = Pattern.compile("class=\"\"pinyin\"\">(.*?)<script>(?:(?!<script>).)*");
Matcher mat_1 = pat_1.matcher( text );
if( mat_1.find() )
{
System.out.println( mat_1.group(1) );
}
//the ubiquitous radical
Pattern pat_2 = Pattern.compile( "<span class=\"\"b\"\">部首:</span>" );
Matcher mat_2 = pat_2.matcher( text );
if( mat_2.find() )
{
Pattern pat_3 = Pattern.compile("<span class=\"\"b\"\">部首:</span>(.*?)<span class=\"\"b\"\">");
Matcher mat_3 = pat_3.matcher( text );
if( mat_3.find() )
{
System.out.println("部首:" + mat_3.group(1) );
}
//stroke count
Pattern pat_4 = Pattern.compile(mat_3.group(1) + "<span class=\"\"b\"\">部首笔画:</span>(.*?)<span class=\"\"b\"\">");
Matcher mat_4 = pat_4.matcher( text );
if( mat_4.find() )
{
System.out.println("笔画:" + mat_4.group(1) );
}
}
else
{
//simple rad
Pattern pat_5 = Pattern.compile("简体部首:</span>(.*?)<span class=\"\"b\"\">");
Matcher mat_5 = pat_5.matcher( text );
if( mat_5.find() )
{
System.out.println("简体部首:" + mat_5.group(1) );
//stroke count
Pattern pat_6 = Pattern.compile(mat_5.group(1) + "<span class=\"\"b\"\">部首笔画:</span>(.*?)<span class=\"\"b\"\">");
Matcher mat_6 = pat_6.matcher( text );
if( mat_6.find() )
{
System.out.println("简体笔画:" + mat_6.group(1) );
}
}
//trad rad
Pattern pat_7 = Pattern.compile("繁体部首:</span>(.*?)<span class=\"\"b\"\">");
Matcher mat_7 = pat_7.matcher( text );
if( mat_7.find() )
{
System.out.println("繁体部首:" + mat_7.group(1) );
//stroke count
Pattern pat_8 = Pattern.compile(mat_7.group(1) + "<span class=\"\"b\"\">部首笔画:</span>(.*?)<span class=\"\"b\"\">");
Matcher mat_8 = pat_8.matcher( text );
if( mat_8.find() )
{
System.out.println("繁体笔画:" + mat_8.group(1) );
}
}
}
//the decomposition
Pattern pat_9 = Pattern.compile("#################,\" ]:(.*?)\(");
Matcher mat_9 = pat_9.matcher( text );
if( mat_9.find() )
{
System.out.println("首尾分解: " + mat_9.group(1) );
}
}
}
}
我无法控制数据的结构。
也许我可以使用某种 LinkedList 对象来填充,并且我可以在每次迭代时填写正确的顺序,最后打印出来。那有意义吗?如果是,那很好,但我不知道如何实际实现类似的东西。如果不是,什么会更好?
当前输出如下所示:
首尾分解: 占乂
卥
xī
简体部首:丨
简体笔画:1
繁体部首:卜
繁体笔画:2
首尾分解: 巛乙
巤
liè
部首:巛
笔画:3
首尾分解: 工页
项
xiàng
简体部首:页
简体笔画:6
繁体部首:頁
繁体笔画:9
我希望它看起来如何:
卥
xī
首尾分解: 占乂
简体部首:丨
简体笔画:1
繁体部首:卜
繁体笔画:2
巤
liè
首尾分解: 巛乙
部首:巛
笔画:3
项
xiàng
首尾分解: 工页
简体部首:页
简体笔画:6
繁体部首:頁
繁体笔画:9
数据的外观:
#######################," ]:占乂(zhancha)
","<table width=""620"" border=""0"" cellpadding=""0"" cellspacing=""0"">
<tr bgcolor=""#FFFFFF"">
<td width=""100""><div id=""zibg""><p class=""U5365""></p></div></td>
<td width=""510"" style=""padding-left:10px"">
<p class=""text15"">
『卥』 <br>
<span class=""b"">拼音:</span><span class=""pinyin"">xī<script>Setduyin('Duyin/xi1')</script></span> <span class=""b"">注音:</span><span class=""pinyin"">ㄒㄧ<script>Setduyin('Duyin/xi1')</script></span><br>
<span class=""b"">简体部首:</span>丨 <span class=""b"">部首笔画:</span>1 <span class=""b"">总笔画:</span>8<br><span class=""b"">繁体部首:</span>卜 <span class=""b"">部首笔画:</span>2 <span class=""b"">总笔画:</span>8<br><span class=""b"">康熙字典笔画</span>( 卥:8; )
</p></td>
</tr>
</table>"
#######################," ]:巛乙(chuanyi)
","<table width=""620"" border=""0"" cellpadding=""0"" cellspacing=""0"">
<tr bgcolor=""#FFFFFF"">
<td width=""100""><div id=""zibg""><p class=""U5DE4""></p></div></td>
<td width=""510"" style=""padding-left:10px"">
<p class=""text15"">
『巤』 <br>
<span class=""b"">拼音:</span><span class=""pinyin"">liè<script>Setduyin('Duyin/lie4')</script></span> <span class=""b"">注音:</span><span class=""pinyin"">ㄌㄧㄝˋ<script>Setduyin('Duyin/lie4')</script></span><br>
<span class=""b"">部首:</span>巛 <span class=""b"">部首笔画:</span>3 <span class=""b"">总笔画:</span>15<br><span class=""b"">康熙字典笔画</span>( 巤:15; )
</p></td>
</tr>
</table>"
但很明显为什么你没有预期的顺序:你正在逐行读取文件,当然你只会在第三个周期获得 pat0 的正确行(所以,在你处理了第一个和第二个)。
您可能应该创建一个实用程序对象,它有助于在解析后重新排列行。问题是在 sort 其行中找到一个组标识符。我看不懂你的字母表,所以我帮不了你。
当你有一个 "group id" 时,你可以创建一个 java.lang.Comparable 对象,它使用组 ID 和模式编号在放入 Set 时具有正确的顺序。在解析结束时,您可以打印出行。
此刻我的代码如下所示。它非常简单,它只是读入一个数据文件并抓取所有有趣的位并打印出来。麻烦的是,它打印出来的方式不对,顺序不对。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class text_processing
{
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException
{
String text;
BufferedReader br = new BufferedReader(new FileReader("/home/matthias/Workbench/SUTD/1_February/brute_force/items.csv"));
while ((text = br.readLine()) != null)
{
//the main character
Pattern pat_0 = Pattern.compile( "『(.*?)』" );
Matcher mat_0 = pat_0.matcher( text );
if( mat_0.find() )
{
System.out.println( mat_0.group(1) );
}
//the pin yin
Pattern pat_1 = Pattern.compile("class=\"\"pinyin\"\">(.*?)<script>(?:(?!<script>).)*");
Matcher mat_1 = pat_1.matcher( text );
if( mat_1.find() )
{
System.out.println( mat_1.group(1) );
}
//the ubiquitous radical
Pattern pat_2 = Pattern.compile( "<span class=\"\"b\"\">部首:</span>" );
Matcher mat_2 = pat_2.matcher( text );
if( mat_2.find() )
{
Pattern pat_3 = Pattern.compile("<span class=\"\"b\"\">部首:</span>(.*?)<span class=\"\"b\"\">");
Matcher mat_3 = pat_3.matcher( text );
if( mat_3.find() )
{
System.out.println("部首:" + mat_3.group(1) );
}
//stroke count
Pattern pat_4 = Pattern.compile(mat_3.group(1) + "<span class=\"\"b\"\">部首笔画:</span>(.*?)<span class=\"\"b\"\">");
Matcher mat_4 = pat_4.matcher( text );
if( mat_4.find() )
{
System.out.println("笔画:" + mat_4.group(1) );
}
}
else
{
//simple rad
Pattern pat_5 = Pattern.compile("简体部首:</span>(.*?)<span class=\"\"b\"\">");
Matcher mat_5 = pat_5.matcher( text );
if( mat_5.find() )
{
System.out.println("简体部首:" + mat_5.group(1) );
//stroke count
Pattern pat_6 = Pattern.compile(mat_5.group(1) + "<span class=\"\"b\"\">部首笔画:</span>(.*?)<span class=\"\"b\"\">");
Matcher mat_6 = pat_6.matcher( text );
if( mat_6.find() )
{
System.out.println("简体笔画:" + mat_6.group(1) );
}
}
//trad rad
Pattern pat_7 = Pattern.compile("繁体部首:</span>(.*?)<span class=\"\"b\"\">");
Matcher mat_7 = pat_7.matcher( text );
if( mat_7.find() )
{
System.out.println("繁体部首:" + mat_7.group(1) );
//stroke count
Pattern pat_8 = Pattern.compile(mat_7.group(1) + "<span class=\"\"b\"\">部首笔画:</span>(.*?)<span class=\"\"b\"\">");
Matcher mat_8 = pat_8.matcher( text );
if( mat_8.find() )
{
System.out.println("繁体笔画:" + mat_8.group(1) );
}
}
}
//the decomposition
Pattern pat_9 = Pattern.compile("#################,\" ]:(.*?)\(");
Matcher mat_9 = pat_9.matcher( text );
if( mat_9.find() )
{
System.out.println("首尾分解: " + mat_9.group(1) );
}
}
}
}
我无法控制数据的结构。
也许我可以使用某种 LinkedList 对象来填充,并且我可以在每次迭代时填写正确的顺序,最后打印出来。那有意义吗?如果是,那很好,但我不知道如何实际实现类似的东西。如果不是,什么会更好?
当前输出如下所示:
首尾分解: 占乂
卥
xī
简体部首:丨
简体笔画:1
繁体部首:卜
繁体笔画:2
首尾分解: 巛乙
巤
liè
部首:巛
笔画:3
首尾分解: 工页
项
xiàng
简体部首:页
简体笔画:6
繁体部首:頁
繁体笔画:9
我希望它看起来如何:
卥
xī
首尾分解: 占乂
简体部首:丨
简体笔画:1
繁体部首:卜
繁体笔画:2
巤
liè
首尾分解: 巛乙
部首:巛
笔画:3
项
xiàng
首尾分解: 工页
简体部首:页
简体笔画:6
繁体部首:頁
繁体笔画:9
数据的外观:
#######################," ]:占乂(zhancha)
","<table width=""620"" border=""0"" cellpadding=""0"" cellspacing=""0"">
<tr bgcolor=""#FFFFFF"">
<td width=""100""><div id=""zibg""><p class=""U5365""></p></div></td>
<td width=""510"" style=""padding-left:10px"">
<p class=""text15"">
『卥』 <br>
<span class=""b"">拼音:</span><span class=""pinyin"">xī<script>Setduyin('Duyin/xi1')</script></span> <span class=""b"">注音:</span><span class=""pinyin"">ㄒㄧ<script>Setduyin('Duyin/xi1')</script></span><br>
<span class=""b"">简体部首:</span>丨 <span class=""b"">部首笔画:</span>1 <span class=""b"">总笔画:</span>8<br><span class=""b"">繁体部首:</span>卜 <span class=""b"">部首笔画:</span>2 <span class=""b"">总笔画:</span>8<br><span class=""b"">康熙字典笔画</span>( 卥:8; )
</p></td>
</tr>
</table>"
#######################," ]:巛乙(chuanyi)
","<table width=""620"" border=""0"" cellpadding=""0"" cellspacing=""0"">
<tr bgcolor=""#FFFFFF"">
<td width=""100""><div id=""zibg""><p class=""U5DE4""></p></div></td>
<td width=""510"" style=""padding-left:10px"">
<p class=""text15"">
『巤』 <br>
<span class=""b"">拼音:</span><span class=""pinyin"">liè<script>Setduyin('Duyin/lie4')</script></span> <span class=""b"">注音:</span><span class=""pinyin"">ㄌㄧㄝˋ<script>Setduyin('Duyin/lie4')</script></span><br>
<span class=""b"">部首:</span>巛 <span class=""b"">部首笔画:</span>3 <span class=""b"">总笔画:</span>15<br><span class=""b"">康熙字典笔画</span>( 巤:15; )
</p></td>
</tr>
</table>"
但很明显为什么你没有预期的顺序:你正在逐行读取文件,当然你只会在第三个周期获得 pat0 的正确行(所以,在你处理了第一个和第二个)。
您可能应该创建一个实用程序对象,它有助于在解析后重新排列行。问题是在 sort 其行中找到一个组标识符。我看不懂你的字母表,所以我帮不了你。
当你有一个 "group id" 时,你可以创建一个 java.lang.Comparable 对象,它使用组 ID 和模式编号在放入 Set 时具有正确的顺序。在解析结束时,您可以打印出行。