文を検索して|for| in | atという単語が含まれているかどうかを確認する単純な正規表現があります。これが、正規表現でほぼ機能している正規表現です。
[^ A-Za-z] of | for | in | at [^ A-Za-z]
私はそれを次の設定で実行します:
show me the weather of seattle
show me the weather in seattle
show me the weather for seattle
show me the weather at seattle
結果は次のとおりです。
Javaコードで使用すると、まったく機能しません。regexpalにも、開始時と終了時に定義したforとatのスペースが表示されます。誰かが私の正規表現の何が問題になっているのか、そして文中の多くの単語の1つを検索する方法を教えてもらえますか
私は常にJavaでelse条件を取得します。これは、正規表現と一致しないことを意味します。これが私のJavaコードです:
public class Regex
{
public static void main(String[] args)
{
String str = "show me the weather in seattle";
if(str.matches("[^A-Za-z]of|for|in|at[^A-Za-z]")){
System.out.println("Yayyy!!");
}
else{
System.out.println("OMG now what to do");
}
}
}