簡體   English   中英

如何從Java中的混亂字符串中獲取文本?

[英]How to grab text from a messy string in java?

我正在讀取一個包含電影標題,年份,語言等的文本文件。我試圖獲取這些屬性。

假設一些字符串是這樣的:

 String s = "A Fatal Inversion" (1992)"
 String d = "(aka "Verhngnisvolles Erbe" (1992))    (Germany)"
 String f =  "\"#Yaprava\" (2013) "
 String g = "(aka \"Love Heritage\" (2002)) (International: English title)"

如何指定標題,年份,國家(如果指定),如何指定標題?

我不太擅長使用正則表達式和模式,但是當不指定它們時,我不知道如何查找該屬性。 我這樣做是因為我試圖從文本文件生成xml。 我有它的dtd,但我不確定在這種情況下是否需要它來使用它。

編輯:這是我嘗試過的。

    String pattern;
    Pattern p = Pattern.compile("\"([^\"]*)\"");
    Matcher m;



    Pattern number = Pattern.compile("\\d+");
    Matcher num;

    m = p.matcher(s);

    num = number.matcher(s);

    if(m.find()){
        System.out.println(m.group(1));
    }

    if(num.find()){
        System.out.println(num.group(0));
    }

我建議您首先提取年份,因為這似乎相當一致。 然后,我提取國家(如果有),其余的就是標題。

為了提取國家/地區,我建議您使用已知國家/地區的名稱對正則表達式模式進行硬編碼。 確定它們是什么可能需要反復進行,因為它們似乎很不一致。

這段代碼有點丑陋(但是數據也是如此!):

public class Extraction {
    public final String original;
    public String year = "";
    public String title = "";
    public String country = "";

    private String remaining;

    public Extraction(String s) {
        this.original = s;
        this.remaining = s;
        extractBracketedYear();
        extractBracketedCountry();
        this.title = remaining;
    }

    private void extractBracketedYear() {
        Matcher matcher = Pattern.compile(" ?\\(([0-9]+)\\) ?").matcher(remaining);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            this.year = matcher.group(1);
            matcher.appendReplacement(sb, "");
        }
        matcher.appendTail(sb);
        remaining = sb.toString();
    }

    private void extractBracketedCountry() {
        Matcher matcher = Pattern.compile("\\((Germany|International: English.*?)\\)").matcher(remaining);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            this.country = matcher.group(1);
            matcher.appendReplacement(sb, "");
        }
        matcher.appendTail(sb);
        remaining = sb.toString();
    }

    public static void main(String... args) {

        for (String s : new String[] {
                "A Fatal Inversion (1992)",
                "(aka \"Verhngnisvolles Erbe\" (1992))    (Germany)",
                "\"#Yaprava\" (2013) ",
                "(aka \"Love Heritage\" (2002)) (International: English title)"}) {

            Extraction extraction = new Extraction(s);
            System.out.println("title   = " + extraction.title);
            System.out.println("country = " + extraction.country);
            System.out.println("year    = " + extraction.year);
            System.out.println();
        }
    }

}

產生:

title   = A Fatal Inversion
country = 
year    = 1992

title   = (aka "Verhngnisvolles Erbe")    
country = Germany
year    = 1992

title   = "#Yaprava"
country = 
year    = 2013

title   = (aka "Love Heritage") 
country = International: English title
year    = 2002

一旦獲得了這些數據,就可以進一步對其進行操作(例如,“國際:英文標題”->“英國”)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM