Java 正則學習筆記

在線測試工具 http://tool.oschina.net/regex/

 

正則,用於檢測字符串是不是符合規則的方法。

     字符串,一共兩種情況:字符本身、字符位置

     檢查符合規則,提供兩種操作:檢查是否符合、提取符合結果

 

Java寫法:

                       Patternp = Pattern.compile("\\d+");

                       Matcherm = p.matcher("heo123");

                       if(m.find())

                                    System.out.println(m.group());

                       else

                                    System.out.println("無");


打印結果:123

API說明:http://tool.oschina.net/uploads/apidocs/jdk-zh/java/util/regex/Pattern.html

 

 

正文開始:

     匹配,當然有兩種:精準匹配、模糊匹配。

 

1. 精準匹配:

      "hello",

     匹配"hello"爲true;

     匹配"hello1"爲false,但matcher.find()返回爲true,matcher.group()返回爲“hello”

     匹配"h1ello"爲false,                                 false,                       報異常:java.lang.IllegalStateException:No match found

 

2.  模糊匹配:

     精準匹配沒什麼意思,助你理解。重點是這個模糊匹配:

     模糊爲兩種:一個字符可以有幾個、一個位置可以是什麼字符。

2.1 一個字符可以有幾個?

      "hel{1,3}o",

     可以匹配的字符串有:helo、hello、helllo

2.2 一個位置可以是什麼字符?

     這裏先說“一個”位置,再說“連續多個”位置

2.2.1 一個位置:

      “hell[a,o]”,

     可以匹配的字符串有:hella、hello

     1) 我想匹配更多,比如:a-z都要可以匹配,即希望hella\hellb\hellc\...\hellz

          hell[a-z],

          但區分大小寫,hellA是不行的

     2) 我大小寫都想要:

          hell[a-zA-Z]

     3) 數字想要

          hell[a-zA-Z0-9]

     4) 想要數字了

          hell[0-9]

     5) 除此之外我還想要"-"

          hell[0-9\\-]    或   hell[0-9-]    反正注意-有特殊使命,別像上面那樣寫在範圍的中間

     6) 我想要空白字符,什麼\b\t\n\f\r的儘管來

          額,那就hell[\b\t\n\f\r]得了唄

\t

製表符 ('\u0009')

\n

新行(換行)符 ('\u000A')

\r

回車符 ('\u000D')

\f

換頁符 ('\u000C')

      7) 我不要[a,b,c]其它的什麼字符都行

         hell[^a-c]

     8) 那我只要[a-z]但不要bc

         hell[a-z&&[^bc]]

     9) 約定的預定義字符類:

預定義字符類

.

任何字符(與行結束符可能匹配也可能不匹配)

\d

數字:[0-9]

\D

非數字: [^0-9]

\s

空白字符:[ \t\n\x0B\f\r]

\S

非空白字符:[^\s]

\w

單詞字符:[a-zA-Z_0-9]

\W

非單詞字符:[^\w]

注意,以上的模式字串,只能匹配一個字符。

 

2.2.2 多個位置:

     1) 我讓hell字符串後面可以隨便有3個英文或數字字符:

     hell[a-zA-Z0-9]{3}

     2)這三個字符寫不寫都不打緊,但不能超過3個:

     hell[a-zA-Z0-9]{0,3}                     //注意,多於3個字符時,find()能找到,group()結果是隻有3個的,但Pattern.matches("hell[a-zA-Z0-9]{0,3}","hellb1b3");的結果是false

      3)隨便幾個字符都行了,不必強求3個,但至少得有一個:

     hell[a-zA-Z0-9]+

     4)那一個也沒有也行:

      hell[a-zA-Z0-9]?

     5)重點來了:

Greedy 數量詞

X?

X,一次或一次也沒有

X*

X,零次或多次

X+

X,一次或多次

X{n}

X,恰好 n 次

X{n,}

X,至少 n 次

X{n,m}

X,至少 n 次,但是不超過 m 次

 

      看看這裏,注意分析:

regix="\d{2,5}"||string="123"||result=123
regix="\d{2,5}"||string="1234"||result=1234
regix="\d{2,5}"||string="12345"||result=12345
regix="\d{2,5}"||string="123456"||result=12345  //不加問號,則貪婪匹配,能多匹配點就多匹配點

regix="\d{2,5}?"||string="123"||result=12        //在數量詞後面加問號,則惰性匹配
regix="\d{2,5}?"||string="1234"||result=12
regix="\d{2,5}?"||string="12345"||result=12
regix="\d{2,5}?"||string="123456"||result=12

regix="\d{2,}?"||string="123"||result=12
regix="\d{2,}?"||string="1234"||result=12
regix="\d{2,}?"||string="12345"||result=12
regix="\d{2,}?"||string="123456"||result=12

regix="\d+?"||string="123"||result=1
regix="\d+?"||string="1234"||result=1
regix="\d+?"||string="12345"||result=1
regix="\d+?"||string="123456"||result=1


3.邊界匹配


// String regix = "^"; //#hello
// String regix = "$"; //hello#
// String regix = "^|$"; //#hello#       #hello world#
// String string = "hello";
// String string = "hello world";

// String regix = "\\b"; //[#JS#] #Lesson_01#.#mp4#
// String regix = "\\B"; //#[J#S]# L#e#s#s#o#n#_#0#1.m#p#4
// String string = "[JS] Lesson_01.mp4";

//正向先行斷言和負向先行斷言   positive lookahead和negative lookahead
//白話解釋:找出指定字符 前面 的位置,加!就是相反的位置
// String regix = "(?=l)"; //he#l#lo
// String regix = "(?=e)"; //h#ello
// String regix = "(?!l)"; //#h#ell#o#
// String string = "hello";

//環視,即看看右邊或看看左邊
//白話解釋:找出指定字符 後面 的位置,加!就是相反的位置
// String regix = "(?<=l)"; //hel#l#o
// String regix = "(?<=e)"; //he#llo
// String regix = "(?<!l)"; //#h#e#llo#
// String regix = "(?<!e)"; //#h#el#l#o#
//沒有以下這種寫法
// String regix = "(?>=l)"; //hello
// String regix = "(?>=e)"; //hello
// String regix = "(?>!l)"; //hello
// String regix = "(?>!e)"; //hello
// String string = "hello";

// String regix = "(?=\\d{3})+"; //,1,2,3,4,5,678 ,1,2,3,4,5,6,789
// String regix = "(?=(\\d{3})+)"; //,1,2,3,4,5,678 ,1,2,3,4,5,6,789
// String regix = "(?=\\d{3}\\b)+"; //12345,678 123456,789
// String regix = "(?=(\\d{3})+\\b)"; //12,345,678 ,123,456,789
// String regix = "(\\B)(?=(\\d{3})+\\b)";
// String string = "12345678 123456789";
// Pattern p = Pattern.compile(regix);
// Matcher m = p.matcher(string);
// String a = m.replaceAll(",");
// //System.out.println(m.group());
// System.out.println(a);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章