English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PatternクラスのDOTALLフィールドはdotallモードを有効にします。デフォルトでは、「。」正規表現のメタキャラクターは行終端以外のすべての文字にマッチします。
import java.util.regex.Matcher; import java.util.regex.Pattern; public class DOTALL_Example { public static void main( String args[] ) { String regex = "."; String input = "this is a sample \nthis is second line"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input); int count =0; while(matcher.find()) { count++; System.out.print(matcher.group()); } System.out.println(); System.out.println("新しい行キャラクタの数: \n"+count); } }
出力結果
this is a sample this is second line 新しい行キャラクタの数: 36
点すべてモードでは、行終端を含むすべての文字にマッチします。
言い換えれば、それを使用する際には、compile()
メソッドのフラグ値が、「。」文字列表現が行終端を含むすべての文字にマッチします。
import java.util.regex.Matcher; import java.util.regex.Pattern; public class DOTALL_Example { public static void main( String args[] ) { String regex = "."; String input = "this is a sample \nthis is second line"; Pattern pattern = Pattern.compile(regex, Pattern.DOTALL); Matcher matcher = pattern.matcher(input); int count = 0; while(matcher.find()) { count++; System.out.print(matcher.group()); } System.out.println(); System.out.println("新しい行キャラクタの数: \n"+count); } }
出力結果
これはサンプル これは2行目 新しい行キャラクタの数: 37