java正則表達式

package cm;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test20180425Regex {

	public static void main(String[] args) {
		Matcher m = Pattern.compile("([a-z]+)",Pattern.CASE_INSENSITIVE|Pattern.UNICODE_CASE).matcher("Bc a absd aaa a a a ");
		//主要的匹配模式(選項):Pattern.CASE_INSENSITIVE|和Pattern.UNICODE_CASE聯用忽略大小寫。
		// System.out.println(m.matches());//匹配器和字符串完全匹配,能返回true;

		// System.out.println(m.lookingAt());//匹配器重字符串開頭進行匹配,如果能匹配返回ture;

		// System.out.println(m.groupCount());// 返回正則表達式有幾個組,不包括0組。
		//
		// while (m.find()) {// find(int index)方法從待匹配的index索引處截取字串進行正則表達式的匹配。find()方法指針自動向後移動,如同迭代器一樣。
		// System.out.println("start:" + m.start());// .start屬性代表find指針開始移動時指向字符串的索引。
		//
		// System.out.println(m.group(1));// group()方法代表正則表達式中有幾個組。無參代表整個正則表達即0組。(a(bc))比如1代表參數(a(bc))組,2代表(bc)組
		//
		// System.out.println("end:" + m.end());// .end屬性代表find指針移動後指向字符串的索引。
		// }
		//
		// System.out.println(m.pattern().toString());// pattern返回m匹配器的模式,調用tostring方法輸出正則表達式的字符串
		//
		// //正則表達式用於分割
		// System.out.println(Arrays.asList(Pattern.compile(" ").split("a bs cc a")));
		//
		// //用於替換匹配到的第一個目標
		// String s="bigseacoming good";
		// s=s.replaceFirst(" {1,}", "_");//替換第一個空格爲下劃線
		// System.out.println(s);
		//
		// //用於替換匹配到的所有目標
		// String b="bigseacoming is very goob";
		// b=b.replaceAll("[b]+", "B");//替換所有目標子字符串;
		// System.out.println(b);
		//
		// //reset方法的使用
		// Matcher ma=Pattern.compile("[a-z]+").matcher("who are you");
		// ma.reset("who am i");//將匹配器的字符串重置爲who am i
		// ma.reset();//將匹配器設置到當前序列的開始狀態。即start指向索引爲0。
		// while(ma.find()) {
		// System.out.println(ma.group());
		// }
		// ma.reset();//這裏的作用就是將find的指針重置爲開始狀態
		// ma.find(0);//這裏將find的指針指向0
		// while(ma.find()) {
		// System.out.println(ma.group());
		// }
		
		
	}

}

發佈了64 篇原創文章 · 獲贊 11 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章