Java 中的 Pattern DOTALL 欄位(含示例)
Pattern 類的 DOTALL 欄位啟用 dotall 模式。預設情況下,正則表示式中的“.”元字元匹配除換行符之外的所有字元。
示例 1
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("Number of new line characters: \n"+count);
}
}輸出
this is a sample this is second line Number of new line characters: 36
在 dot all 模式中,它匹配所有字元,包括換行符。
換句話說,當你將此用作 compile() 方法的標誌值時,“.”元字元將匹配所有字元,包括換行符。
示例 2
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("Number of new line characters: \n"+count);
}
}輸出
this is a sample this is second line Number of new line characters: 37
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP