首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >细化java模式匹配函数以以不同的顺序打印,可能的链接列表操作

细化java模式匹配函数以以不同的顺序打印,可能的链接列表操作
EN

Stack Overflow用户
提问于 2015-02-12 10:17:08
回答 1查看 127关注 0票数 1

在这一刻,我的代码如下所示。这很简单,它只是读取一个数据文件,然后抓取所有有趣的部分并打印出来。问题是,打印出来的方式是错误的,顺序是不正确的。

代码语言:javascript
复制
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对象来填充,并且我可以在每次迭代时填写正确的顺序,最后打印出来。这说得通吗?如果是的话,那很好,但我不知道如何真正实现这样的东西。如果没有,还有什么更好呢?

当前的输出如下:

代码语言:javascript
复制
首尾分解: 占乂
卥
xī
简体部首:丨 
简体笔画:1 
繁体部首:卜 
繁体笔画:2 
首尾分解: 巛乙
巤
liè
部首:巛 
笔画:3 
首尾分解: 工页
项
xiàng
简体部首:页 
简体笔画:6 
繁体部首:頁 
繁体笔画:9 

我想让它看上去是:

代码语言:javascript
复制
卥
xī
首尾分解: 占乂
简体部首:丨 
简体笔画:1 
繁体部首:卜 
繁体笔画:2 

巤
liè
首尾分解: 巛乙
部首:巛 
笔画:3 

项
xiàng
首尾分解: 工页
简体部首:页 
简体笔画:6 
繁体部首:頁 
繁体笔画:9

数据的外观:

代码语言:javascript
复制
#######################," ]:占乂(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>"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-12 10:36:01

但很明显,您没有预期的顺序:逐行读取文件,当然只有在第三个周期(在处理第一个和第二个循环之后)才能得到正确的pat0行。

您可能应该创建一个实用程序对象,它有助于解析后重新排列行。问题是如何在对其行进行排序时找到组标识符。我看不懂你的字母表,所以我不能帮你。

当您有一个“组id”时,您可以创建一个java.lang.Comparable对象,该对象使用组id和模式号,以便在将其放入Set时得到正确的顺序。在解析结束时,您可以打印出行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28474801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档