曾處理Oracle一項產品叫OGG(Oracle Golden Gate),它產出一個定義檔javaue.def。長相如下:
*
*
Definition for table TESTUTF8.T1
Record length: 112
Syskey: 0
Columns: 2
C1 64 50 0 0 0 1 0 50 50 50 0 0 0 0 1 0 1 2
C2 64 50 56 0 0 1 0 50 50 0 0 0 0 0 1 0 0 0
End of definition
* TEST T2
Definition for table TESTUTF8.T2
Record length: 112
Syskey: 0
Columns: 2
C1 64 50 0 0 0 1 0 50 50 50 0 0 0 0 1 0 1 2
C2 64 50 56 0 0 1 0 50 50 0 0 0 0 0 1 0 0 0
End of definition
要取得符合從Definition for table到End of definition內容,怎麼取得?因為有跨行情況,所以在Java上可以這樣寫:
import java.io.FileInputStream;
import java.io.StringReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestRegexMultiLines {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("./javaue.def");
byte[] ba = new byte[fis.available()];
fis.read(ba);
String javadef = new String(ba);
Pattern pat = Pattern.compile("Definition for table .*?End of definition", Pattern.DOTALL);
Matcher mat = pat.matcher(javadef);
while (mat.find()) {
System.out.println("========================");
System.out.println(mat.group());
}
}
}
主要是在Pattern.compile第二個參數要帶Pattern.DOTALL,才能進行跨行比對。而.*?
這個不貪多量詞常被筆者用在各個專案和或維運上。
若不帶第二個參數也是可以,改用(?s)
,例:
Pattern pat = Pattern.compile("(?s)Definition for table .*?End of definition");