2014-10-03 59 views
1

我有這樣的字符串:分裂由java的正則表達式

Snt:It was the most widespread day of environmental action in the planet's history 
==================== 
----------- 
Snt:Five years ago, I was working for just over minimum wage 
==================== 
----------- 

,我想串與

==================== 
----------- 

分裂和ofcourse從第一個句子中刪除Snt:。 什麼是最好的方法?

我用這個正則表達式,但它沒有工作!

String[] content1 =content.split("\\n\\====================\\n\\-----------\\n"); 

在此先感謝。

+1

使用'content.replaceAll( 「SNT」, 「」);'然後做分割 – Tirath 2014-10-03 16:56:47

+1

這可能不是split'的'物盡其用。你是從文件中讀取這些行嗎?也許檢查你從'BufferedReader'返回的行真的是你想要做的。 – 2014-10-03 17:02:46

回答

1

因爲最後沒有換行符,所以不會匹配最後的==,--行。您需要在最後添加最後一行代碼$,作爲\n的替代方案。

String s = "Snt:It was the most widespread day of environmental action in the planet's history\n" + 
"====================\n" + 
"-----------\n" + 
"Snt:Five years ago, I was working for just over minimum wage\n" + 
"====================\n" + 
"-----------"; 
String m = s.replaceAll("(?m)^Snt:", ""); 
String[] tok = m.split("\\n\\====================\\n\\-----------(?:\\n|$)"); 
System.out.println(Arrays.toString(tok)); 

輸出:

[It was the most widespread day of environmental action in the planet's history, Five years ago, I was working for just over minimum wage] 
+2

爲什麼downvote? – 2014-10-03 17:04:03

2

的因爲數據是結構化的,我就從一個分裂扭轉的概念,是一個匹配,而不是方法,這可以讓你mathc的Snt很好地爲好:

private static final String VAL = "Snt:It was the most widespread day of environmental action in the planet's history\n" 
     + "====================\n" 
     + "-----------\n" 
     + "Snt:Five years ago, I was working for just over minimum wage\n" 
     + "====================\n" 
     + "-----------"; 

public static void main(String[] args) { 
    List<String> phrases = new ArrayList<String>(); 
    Matcher mat = Pattern.compile("Snt:(.+?)\n={20}\n-{11}\\s*").matcher(VAL); 
    while (mat.find()) { 
     phrases.add(mat.group(1)); 
    } 

    System.out.printf("Value: %s%n", phrases); 
} 

我用正則表達式:"Snt:(.+?)\n={20}\n-{11}\\s*"

假設文件中的第一個單詞是Snt:,然後它將下一個短語分組,直到分隔符爲止。它將消耗任何尾隨的空白,使表達式爲下一個記錄做好準備。

這個過程的好處是匹配匹配單個記錄,而不是匹配一個記錄的末尾部分的表達式,也許是下一個記錄的開始。

3

Pattern p = Pattern.compile("^Snt:(.*)$", Pattern.MULTILINE); 
Matcher m = p.matcher(str); 

while (m.find()) { 
    String sentence = m.group(1); 
} 

,而不是split各地黑客和做額外的解析,這只是看起來什麼用「SNT」,然後捕獲任何如下開始的行。

+2

您忘記使用'Pattern.MULTILINE'標誌讓'$'匹配行尾而不是僅僅結束字符串。無論如何+1,除非我們想要忽略結果數組中的第一個元素,因爲還需要刪除'Snt:',否則這不能用'split'合理完成。 – Pshemo 2014-10-03 17:14:15

0
Matcher m = Pattern.compile("([^=\\-]+)([=\\-]+[\\t\\n\\s]*)+").matcher(str); 
while (m.find()) { 
    String match = m.group(1); 
    System.out.println(match); 
}