2011-10-31 193 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*) 
相关问题