Java經典實例:在文本中匹配換行符


默認情況下,正則表達式 ^ 和 $ 忽略行結束符,僅分別與整個輸入序列的開頭和結尾匹配。如果激活 MULTILINE 模式,則 ^輸入的開頭行結束符之后(輸入的結尾)才發生匹配。處於 MULTILINE 模式中時,$ 僅在行結束符之前輸入序列的結尾處匹配。

import java.util.regex.Pattern; /** * Created by Frank * 使用正則表達式在文本中查找換行符 */
public class NLMatch { public static void main(String[] args) { String input = "I dream of engines\nmore engines, all day long"; System.out.println("INPUT:" + input); System.out.println(); String[] patt = {"engines.more engines", "ines\nmore", "engines$"}; for (int i = 0; i < patt.length; i++) { System.out.println("PATTERN:" + patt[i]); boolean found; Pattern p1l = Pattern.compile(patt[i]); found = p1l.matcher(input).find(); System.out.println("DEFAULT match " + found); // .代表任何符號(DOT ALL),
            Pattern pml = Pattern.compile(patt[i], Pattern.DOTALL | Pattern.MULTILINE); found = pml.matcher(input).find(); System.out.println("Multiline match " + found); System.out.println(); } } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM