2011-10-31 207 views
0

我得到了約2000個關於死亡的句子,我想過濾它們的原因。 首先,我要開始與這些:正則表達式需要包含1個單詞

______ fell (*) ______ 

to the 
off the 
from the 

其中______是一組1個字,並且(*)關閉

我試圖

(\w*)fell+\s+to\sthe|off\sthe|from\sthe(\w*) 

它返回「關」等,但它不看,如果這個詞倒下是那裏。 (這兩個組可能都不工作)

那麼怎麼了,我使用fell+所以下降應該有一次嗎?

回答

0

我會去與(\\w*)fell\\s[to|off|from\\sthe]\\s*(\\w*)

這裏有一個小例子:

import java.util.regex.*; 
class rtest { 
    static String regex = "(\\w*)fell\\s[to|off|from\\sthe]\\s*(\\w*)"; 
    static Pattern pattern = Pattern.compile(regex); 

    public static void main(String[] args) { 
     process("Bob fell off the bike"); 
     process("Matt fell to the bottom"); 
     process("I think Terry fell from the beat of a different drum"); 
    } 
    static void process(String text) { 
     System.out.println(text); 
     String[] tokens = text.split(regex); 
     for(String t : tokens) System.out.println(t); 
     System.out.println(" "); 
    } 
} 

結果:

C:\Documents and Settings\glowcoder\My Documents>javac rtest.java 

C:\Documents and Settings\glowcoder\My Documents>java rtest 
Bob fell off the bike 
Bob 
the bike 

Matt fell to the bottom 
Matt 
the bottom 

I think Terry fell from the beat of a different drum 
I think Terry 
the beat of a different drum 
+0

既感謝,併爲後期的答案對不起,我有點新的棧溢出 – clankill3r

1

您需要周圍的交替選項括號:

(\w*)fell\s(to\sthe|off\sthe|from\sthe)(\w*) 

爲了避免捕獲組使用(?: ...)

(\w*)fell\s(?:to\sthe|off\sthe|from\sthe)(\w*) 
相關問題