例を使用したJavaのパターンUNIX_LINESフィールド
このフラグは、Unix回線モードを有効にします。 Unixラインモードでは、「\ n」のみがラインターミネータとして使用され、「\r」はリテラル文字として扱われます。
例1
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";
//Regular expression to accept date in MM-DD-YYY format
String regex = "^T.*e";
//Creating a Pattern object
Pattern pattern = Pattern.compile(regex, Pattern.UNIX_LINES);
//Creating a Matcher object
Matcher matcher = pattern.matcher(input);
int count = 0;
while(matcher.find()) {
count++;
System.out.println(matcher.group());
}
System.out.println("Number of matches: "+count);
}
} 出力
This is the first line This is the second line This is the third line Number of matches: 1
通常モードでは、\rはキャリッジリターンとして扱われます。
例2
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";
//Regular expression to accept date in MM-DD-YYY format
String regex = "^T.*e";
//Creating a Pattern object
Pattern pattern = Pattern.compile(regex);
//Creating a Matcher object
Matcher matcher = pattern.matcher(input);
int count = 0;
while(matcher.find()) {
count++;
System.out.println(matcher.group());
}
System.out.println("Number of matches: "+count);
}
} 出力
This is the first line Number of matches: 1
-
例を含むJavaのパターンCOMMENTSフィールド
PatternクラスのCOMMENTSフィールドでは、パターンに空白とコメントを含めることができます。これをcompile()メソッドのフラグ値として使用する場合、指定されたパターンでは、空白と#で始まるコメントは無視されます。 例1 import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class COMMENTES_Example { public static void main( String args[] ) { &nb
-
例を使用したJavaのパターンCANON_EQフィールド
PatternクラスのCANON_EQフィールドは、正規に等しい場合にのみ2つの文字に一致します。これをcompile()メソッドのフラグ値として使用すると、完全な正規分解が等しい場合にのみ、2つの文字が一致します。 正規分解がUnicodeテキスト正規化形式の1つである場合 例1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class CANON_EQ_Example { public static void main( String args[] ) {