java核心技術卷2 1 8 正規表示式

2021-09-01 18:09:03 字數 2225 閱讀 5455

正規表示式

2023年11月23日 星期五

下午3:57

通過pattern pattern = pattern.compile(str)創造正規表示式

matcher matcher = pattern.matcher(input)來獲得乙個匹配matcher

matcher.matches()全部匹配 matcher.find()部分匹配

matcher.groupcount() 獲得群組個數

matcher.start(i) matcher.end(i) 第i個群組的開始和結束

例:((1?[0-9]):([0-5][0-9]))[ap]m

(時間的正規表示式)

輸入11:59am

群組索引 開始 結束 字串

0 0 7 11:59am

1 0 5 11:59

2 0 2 11

3 3 5 59

通過部分匹配尋找匹配內容

while(matcher.find())

正規表示式匹配url位址並列印

string patternstring = 「"]*"|[\s>])\s>";

pattern pattern = pattern.compile(patternstring,pattern.case_insensitive);

matcher matcher = pattern.matcher(input);

while(matcher.find())

正規表示式的表示方法:

正規表示式替換字串,將數字序列替換為#字元

pattern pattern = pattern.compile("[0-9]+");

matcher matcher = pattern.matcher(input);

string output = matcher.replaceall("#");

正規表示式分隔字串

pattern pattern = pattern.compile("\s*\p\s*");

string tokens = pattern.split(input);

\p 標點符號:!"#$%&』()*+,-./:;<=>?@^_`~

將字串中的標點符號位置以及前後的空格去掉之後,字串陣列輸出

static pattern compile(string expression)

static pattern compile(string expression, int flags)

把正規表示式字串編譯到乙個用於快速出了匹配的模式物件中

matcher matcher(charsequence input)

返回乙個matcher物件,用它來匹配

string split(charsequence input)

string split(charsequence input, int limit)

將輸入分割成標記,模式指定了分隔符的形式。返回標記陣列。limit為產生字串的最大數量

boolean matches()

匹配則返回ture

boolean lookingat()

如果輸入的開頭匹配模式,則返回ture

boolean find()

boolean find(int start)

嘗試找匹配(部分匹配),start是開始找的位置

int start()

int start(int groupindex)

int end()

int end(int groupindex)

返回當前匹配的開始索引和結尾索引的位置,引數表示第幾個群組

string group()

string group(int groupindex)

返回當前匹配,引數表示第幾個群組

string replaceall(string replacement)

string replacefirst(string replacement)

返回從匹配器輸入獲得的通過將所有匹配或第乙個匹配用替換字串替換後的字串

static string quotereplacement(string str)

引用str中的所有\和$

matcher reset()

matcher reset(charsequence input)

復位匹配器的狀態。第二個方法將使匹配器作用於另乙個輸入

Java核心技術卷之位操作

處理整型型別時,可以對組成整型數值的各個位完成操作。位運算子包括 and or xor not 這些運算子按位模式處理 基本用法 public static void main string args 26的二進位制表示為11010,與1000進行 操作,第四位為1,其餘為零,轉化為十進位制是8 當...

java核心技術卷1 陣列集合互轉 note2

檢視 如listlist arrays.aslist you are funny aslist返回的物件不是arraylist,是乙個檢視物件,帶有訪問底層陣列的get和set方法.改變陣列大小的所有方法,都會丟擲乙個unsupported operationexception異常.可以建立乙個帶構...

Java核心技術卷I 基本資料型別

型別 儲存需求 int4位元組 short 2位元組long 8位元組byte 1位元組型別 儲存需求 double 8位元組float 4位元組所有的浮點數值計算都遵循 ieee 754 規範。具體來說,下面是用於表示溢位和出錯情況的三個特殊的浮點數值 正整數除以 0 的結果為正無窮大。計算 0 ...