Javaの正規表現\Zメタ文字
部分式/メタ文字「\Z」は、許容される最終行のターミネータを除く文字列全体の末尾に一致します。
例1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main( String args[] ) {
String regex = "Tutorialspoint\\z";
String input = "Hi how are you welcome to Tutorialspoint";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
int count = 0;
while(m.find()) {
count++;
}
System.out.println("Number of matches: "+count);
}
} 出力
Number of matches: 1
例2
次のJavaプログラムは、指定された入力テキストが数字で終わっているかどうかを確認します。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Data {
public static void main( String args[] ) {
String regex = "[0-9]\\z";
String input = "Hi how are you \n this is sample text \n this is third line 554";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
if(m.find()) {
System.out.println("Given input ends with a digit");
} else {
System.out.println("Given input doesn’t end with a digit");
}
}
} 出力
Given input ends with a digit
-
Javaの正規表現$(ドル)メタ文字
部分表現/メタ文字「$ 」は行の終わりに一致します。 例1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class EndWith { public static void main( String args[] ) { String regex = "Tutorialspoint$"; String input = "Hi how are you welco
-
正規表現^(caret)Javaのメタ文字
部分表現/メタ文字“ ^” 行の先頭に一致します。これを正規表現で使用すると、入力文字列の後続の文と一致します。 例1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "^Hi how are you"; Strin