在 Java 中使用帶有示例的 Pattern 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP