賢明な優れたパフォーマンスとは何ですか:
string.matches("regex")
または
Pattern.compile("regex").matches(string).find()
は? String.matches(String regex)
のPattern.java
Pattern.matches( "正規表現")
2
A
答えて
4
実装にString.java
のmatches()
、またはAPIを介してマッチングされた私は何を参照しています
:Pattern.matches(String regex, CharSequence input)
の
public boolean matches(String regex) {
return Pattern.matches(regex, this);
}
は、インプリメンテーション:
public static boolean matches(String regex, CharSequence input) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
return m.matches();
}
結論:str.matches(regex)
i と全く同じをPattern.compile(regex).matcher(str).matches()
とします。
注:matches()
と同じですが、find()
と同じではありません。あなたはMatcher
にアクセスする必要が
:
Pattern.compile()
を使用してがあれば必要な/良いです。
など。キャプチャグループの結果が必要です。同じ
matches(regex)
を何度も呼び出します。
regex
パターンをコンパイルするだけで、パフォーマンスが向上します。
0
正規表現を再利用するかどうかによって異なります。もしそうなら、一度だけコンパイルするほうがいいです。 String
でmatches
機能はPattern.matches
で定義されています
public boolean matches(String regex) {
return Pattern.matches(regex, this);
}
スニペットopenJDK7
関連する問題
- 1. 正規表現の正規表現の正規表現
- 2. 正規表現 - 正規表現
- 3. 。NET正規表現(正規表現)
- 4. 正規表現の正規表現と ' -
- 5. 正規表現の正規表現データ
- 6. 正規表現 - 正規表現
- 7. 正規表現(正規表現)Java
- 8. 正規表現の正規表現
- 9. C#正規表現の正規表現
- 10. (?=正規表現)VS(?:正規表現)
- 11. 正規表現用の正規表現
- 12. preg_match正規表現の正規表現を持つ正規表現
- 13. 正規表現正規表現と異なるハイブ正規表現ですか?
- 14. 正規表現:表現
- 15. 正規表現表現
- 16. 正規表現表現
- 17. 正規表現:表現
- 18. 正規表現)
- 19. 正規表現
- 20. 正規表現
- 21. 正規表現
- 22. 正規表現
- 23. 正規表現
- 24. 正規表現
- 25. 正規表現
- 26. 正規表現
- 27. 正規表現
- 28. 正規表現
- 29. 正規表現
- 30. 正規表現
回答から:それは依存しています。 – Tom
リンゴ対オレンジのパフォーマンスを比較できません。 'matches()'と 'find()'は同じ動作ではありません。 ---また、 'matches()'ではなく 'compile()'と 'find()'の間に 'matcher()'を置かなければならないので、コードはコンパイルされません。 – Andreas