JavaRegExを使用して特定の文字列/行の終わりを一致させる方法
メタ文字「$」は特定の文字列の末尾に一致します。つまり、文字列の最後の文字に一致します。たとえば、
-
「\\d $」という表現 」は、数字で終わる文字列/行に一致します。
-
「[a-z]$」という表現 」は、小文字のアルファベットで終わる文字列/行に一致します。
例1
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String args[]) {
//Reading String from user
System.out.println("Enter a String");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String regex = ".*[^a-zA-Z0-9//s]$";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
//Retrieving the matcher object
Matcher matcher = pattern.matcher(input);
if(matcher.matches()) {
System.out.println("Match occurred");
} else {
System.out.println("Match not occurred");
}
}
} 出力1
Enter a String this is sample text# Match occurred
出力2
Enter a String hello how are you Match not occurred
例2
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main( String args[] ) {
String regex = "\\.$";
Scanner sc = new Scanner(System.in);
System.out.println("Enter 5 input strings: ");
String input[] = new String[5];
for (int i=0; i<5; i++) {
input[i] = sc.nextLine();
}
//Creating a Pattern object
Pattern p = Pattern.compile(regex);
for(int i=0; i<5;i++) {
//Creating a Matcher object
Matcher m = p.matcher(input[i]);
if(m.find()) {
System.out.println("String "+i+" ends with '.'");
}
}
}
} 出力
Enter 5 input strings: hello how are you. where do you live what is your name. welcome to tutorialspoint The Biggest Online Tutorials Library. String 0 ends with '.' String 2 ends with '.' String 4 ends with '.'
-
Javaを使用してOpenCVで線を引く方法は?
Java OpenCVライブラリのorg.opencv.imgprocパッケージには、Imgprocという名前のクラスが含まれています。線を引くには、 line()を呼び出す必要があります このクラスのメソッド。このメソッドは、次のパラメーターを受け入れます- 線を引く画像を表すマットオブジェクト。 線が引かれるポイントを表す2つのPointオブジェクト。 線の色を表すScalarオブジェクト。 (BGR) 線の太さを表す整数(デフォルト:1)。 例 import org.opencv.core.Core; import org.opencv.core.Mat; i
-
JavaRegExを使用して任意の文字を照合する方法
メタ文字「。」 Javaの正規表現は、任意の文字(単一)に一致します。アルファベット、数字、または任意の特殊文字にすることができます。 例1 import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String args[]) { //Reading String from user