如何在字符串中查找非特定子字符串?

时间:2017-03-14 13:55:45

标签: java string algorithm arraylist substring

我在这里使用搜索功能无法找到这个问题所以我希望有人可以帮助我。

我目前正在编写电视指南计划的“现在和将来”部分,作为小组项目的一部分。我有第一部分完成(显示现在和下一个程序),但因为它是一个精简部分,我不能包括该程序的完整描述。该程序使用以下部分从在线XML文件中读取:Title,StartTime,EndTime和Desc。 在Desc部分中,您有程序的描述,这里有3个单独的例子: -

英国广播公司早餐团队的最新消息,体育,商业和天气...

12/20。消费者计划。 Matt Allwright前往肯特参加检查时的住房官员......

5/6。警察戏剧系列。当DS史蒂夫·阿诺特受到审查时,AC-12的忠诚分歧......

正如您所看到的,这些都是字符串,但我需要提出一种搜索这些字符串的ArrayList的算法,隔离程序系列链接信息子字符串(例如5/6),同时忽略不要删除的描述; t有链接信息。使这一点特别困难的是,系列链接有两种类型:双位数(12/20)和单位数(5/6),但还有其他类型如5/12或52/105或12/12。如何编写一段代码来查找所有这些代码(并显示它们),同时忽略那些没有它们的程序描述?

谢谢。

编辑:我必须补充一下,我是一名新手程序员。我不确定&#34等事物是什么意思;只需使用正则表达式"。

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,我会为你写一个示例程序。

        Pattern startsWithPattern = Pattern.compile("(^\\(?\\d+\\/\\d+\\)?\.)");
    List<String> listOfPrograms = new ArrayList<>();
    List<String> filteredList = new ArrayList<>();
    listOfPrograms.add("The lastest news, sport, business and weather from the BBC's breakfast team...");
    listOfPrograms.add("12/20. Consumer programme. Matt Allwright travels to Kent to join housing officers on inspection...");
    listOfPrograms.add("5/6. Police Drama series. AC-12's loyalties are divided when DS Steve Arnott comes under scrutiny...");
    listOfPrograms.add("(5/6). Police Drama series. AC-12's loyalties are divided when DS Steve Arnott comes under scrutiny...");

    for (String program : listOfPrograms)
    {
        Matcher matcher = startsWithPattern.matcher(program);

        if (matcher.find())
            filteredList.add(program);
    }

答案 1 :(得分:0)

所以基本上你想检查一个字符串是否以 <强>数/号码。 我知道使用正则表达式会更容易,更容易出错并且更简洁,但这里没有正则表达式,只是为了好玩。

image.reprocess!