ファイサル:
私が探してみましたが、私にはどんな意味を成していた何かを見つけることができませんでした!私は、正規表現でのnoob午前:)
特定の単語「some_text」は別の文字列に存在するかどうかを確認しようとしています。
String s = "This is a test() function"
String s2 = "This is a test () function"
上記の2つの文字列を仮定すると、私はで次のパターンを使用して、これを検索することができます正規表現ツール
[^\w]test[ ]*[(]
しかし、使用してJavaで正のマッチを取得できません
System.out.println(s.matches("[^\\w]test[ ]*[(]");
私は4ダブル\とさえ\\エスケープ文字としてで試してみましたが、何も実際に動作しません。
要件は、そう、これらすべての「テスト()、テスト()またはテスト()」を取得する必要があること、単語がスペースで始まっまたは行の最初の単語であり、その特定の単語の後に開き括弧を「(」が確認することです正の試合。
Javaの1.8を使用して
乾杯、ファイサル。
VS97:
あなたが不足している点は、Javaがということですmatches()
置き^
開始時と$
あなたのための正規表現の終わり。:だからあなたの式は、実際と見られています
^[^\w]test[ ]*[(]$
決してあなたの入力を一致させるつもりはないされています。
あなたの要件の説明から行く、私はこのような何かにあなたの正規表現式を再加工することをお勧め(「特定の単語」あなたは意味によって仮定test
):
(?:.*)(?<=\s)(test(?:\s+)?\()(?:.*)
説明:
^ Start of line - added by matches()
(?:.*) Non-capturing group - match anything before the word, but dont capture into a group
(?<=\s) Positive lookbehind - match if word preceded by space, but dont match the space
( Capturing group $1
test(?:\s+)? Match word test and any following spaces, if they exist
\( Match opening bracket
)
(?:.*) Non-capturing group - match rest of string, but dont capture in group
$ End of line - added by matches()
コードサンプル:
public class Main {
public static void main(String[] args) {
String s = "This is a test() function";
String s2 = "This is a test () function";
System.out.println(s.matches("(?:.*)((?<=\\s))(test(?:\\s+)?\\()(?:.*)"));
//true
}
}