English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
このフラグはUnix行モードを有効にします。Unix行モードでは、'\n'のみを行終端として扱い、'\r'を文字として認識します。
import java.util.regex.Matcher; import java.util.regex.Pattern; public class LTERAL_Example { public static void main(String[] args) { String input = "This is the first line\r" + "This is the second line\r" + "This is the third line\r" //正規表現はMM-DD-YYY形式で日付を受け入れます String regex = "^T.*e"; //Pattern オブジェクトを生成します Pattern pattern = Pattern.compile(regex, Pattern.UNIX_LINES); //Matcher オブジェクトを生成します Matcher matcher = pattern.matcher(input); int count = 0; while(matcher.find()) { count++; System.out.println(matcher.group()); } System.out.println("マッチ数の数: ");+count); } }
出力結果
これは最初の行です これは第二行です これは第三行です マッチ数の数: 1
そして通常モードでは、\rは改行として扱われます。
import java.util.regex.Matcher; import java.util.regex.Pattern; public class LTERAL_Example { public static void main(String[] args) { String input = "This is the first line\r" + "This is the second line\r" + "This is the third line\r" //正規表現はMM-DD-YYY形式で日付を受け入れます String regex = "^T.*e"; //Pattern オブジェクトを生成します Pattern pattern = Pattern.compile(regex); //Matcher オブジェクトを生成します Matcher matcher = pattern.matcher(input); int count = 0; while(matcher.find()) { count++; System.out.println(matcher.group()); } System.out.println("マッチ数の数: ");+count); } }
出力結果
これは最初の行です マッチ数の数: 1