国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java 正則表達式

2019-11-08 00:29:26
字體:
供稿:網(wǎng)友

1. 概述

正則表達式:符合一定規(guī)則的字符串。 一般不常用。但要是用起來,卻能解決大問題。在這里對正則表達式整理一下。

2. 正則表達式相關(guān)類介紹

Pattern類:

A compiled rePResentation of a regular expression. 正則表達式的編譯表現(xiàn)形式。

A regular expression, specified as a string, must first be compiled into an instance of this class. The resulting pattern can then be used to create a Matcher object 正則表達式首先要被編譯為Pattern類的對象。可以通過編譯后的Pattern對象創(chuàng)建匹配器Matcher對象。該Matcher對象可以用來匹配字符串。

Matcher類:

An engine that performs match Operations on a character sequence by interpreting a Pattern. 通過該指定Pattern的匹配引擎可以對一個字符串進行多種匹配操作,有如下幾種:

The matches method attempts to match the entire input sequence against the pattern. 匹配整個字符串,matches()方法。

The lookingAt method attempts to match the input sequence, starting at the beginning, against the pattern. 始終從頭開頭匹配,找到為止。lookingAt()方法。

The find method scans the input sequence looking for the next subsequence that matches the pattern. 每次重置,從指定index開始查找。常和group()方法連用。

java Pattern Java Matcher Android Pattern Android Matcher

正則表達式標準寫法:

String類中,正則表達式源碼全都按照此步驟,最終都調(diào)用Matcher類方法。 可查看源碼更詳細獲知。

//把正則表達式編譯成模式對象Pattern Pattern p = Pattern.compile("a*b"); //通過模式對象生成相應的匹配器 Matcher m = p.matcher("aaaaab"); //調(diào)用方法進行匹配 boolean b = m.matches();

3. 常見正則表達式規(guī)則

查看API文檔,得知更詳細的寫法。這里列出常用的。

Java中常用的正則規(guī)則
規(guī)則 含義
字符Characters:
x The character x,字符x
// The backslash character,反斜線
/n The newline (line feed) character (‘/u000A’),另起一行
/r The carriage-return character (‘/u000D’),回車
字符類Character classes:
[abc] a, b, or c。 a,b,或者c
[^abc] Any character except a, b, or c (negation)。除了a,b,c
[a-zA-Z] a through z or A through Z, inclusive (range)。所有的英文字母
預定義字符類Predefined character classes:
. Any character,通配符。 /.表示字符串
/d A digit: [0-9]。所有數(shù)字
/D A non-digit: [^0-9]。非數(shù)字
/s A whitespace character: [ /t/n/x0B/f/r]。所有空字符
/w A Word character: [a-zA-Z_0-9]。所有的英文字母和阿拉伯數(shù)據(jù),和下劃線。
邊界匹配Boundary matchers:
^ The beginning of a line。開頭
$ The end of a line。結(jié)尾
/b A word boundary。單詞邊界。/b詳解
數(shù)量詞Greedy quantifiers:
X? X, once or not at all。至多0次
X* X, zero or more times。至少0次
X+ X, one or more times。至少1次。
X+ X, one or more times。至少1次。
X{n} exactly n times。只有n次。
X{n,} X, at least n times。至少n次。
X{n,m} X, at least n but not more than m times。至少n次,不超過m次。

4. 正則表達式應用

用于判斷 s.matches(“regex”);用于分割 s.split(“regex”);用于替換 s.replaceAll(regex, replacement); s.replaceFirst(regex, replacement)用于獲取 主要通過調(diào)用Matcher類的group()方法獲取匹配的字符串。可查看Matcher類獲取更多。

5. 示例:

判斷手機號:[1][3578]//d{9}判斷郵箱: /w+@(/w+./w+)+判斷中文:[/u4e00-/u9fa5]+ 替換中文CommonUtil獲取所有英文單詞: Pattern pattern = Pattern.compile("http://b+//w+//b+"); Matcher matcher = pattern.matcher(sourceString); while (matcher.find()) { System.out.println(matcher.group()); }
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 蒙山县| 丹寨县| 友谊县| 清远市| 舒城县| 齐河县| 古蔺县| 奉化市| 英山县| 通州市| 龙南县| 高阳县| 景泰县| 白水县| 修文县| 威海市| 读书| 安顺市| 班玛县| 伊吾县| 苍溪县| 梁河县| 弋阳县| 裕民县| 白玉县| 集贤县| 庐江县| 深水埗区| 上高县| 焦作市| 张北县| 佳木斯市| 海阳市| 神木县| 佳木斯市| 依兰县| 麻栗坡县| 六盘水市| 石台县| 伊金霍洛旗| 建水县|