object regexTest {
def findWords(content: String): List[String] =
("hello".r findAllIn content).toList
def main(args: Array[String]) {
val a1 = "hello, i am LEGOTIME ,my blog site is http://blog.csdn.net/legotime,hello everyone!! ?"
//---------------------------匹配单个字符串--------------------------------------------
/** * 匹配单个字符 * findAllIn :全部匹配出来 * findFirstIn : 匹配第一个 */ val p1 = """hello""".r //"hello".r ,也可以
//val dateP1 = """(\d\d\d\d)-(\d\d)-(\d\d)""".r
println(p1 findAllIn a1 toList) //List(hello, hello)
println(p1 findFirstIn a1 toList) //List(hello)
/** * 匹配任意字符串 * . :英文中的句号,可以匹配任意一个单个的字符,可以使用n个 点 来匹配n个字符,同时可以出现在不同位置 */ val p2 = ".g.".r
println(p2 findAllIn a1 toList) //List(og , og., ego)
println(p2 findFirstIn a1 toList) //List(og )
/** * 匹配特殊字符 * \ :转意字符 ,前面加這个 表示有特殊的含义 */ val p3 = """.g\.""".r
println(p3 findAllIn a1 toList) //List(og.)
val p4 = """/""".r
println(p4 findAllIn a1 toList) //List(/, /, /)
//---------------------------匹配一组字符串--------------------------------------------
val a2 = "<div id=\"yui3-css-stamp\" style=\"position: absolute !important; visibility: hidden !important\" class=\"\"></div>" +
"body, Div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, textarea, p, blockquote, th, td"
val p5 = """.i.""".r
println(p5 findAllIn a2 toList) //List(div, id, ui3, sit, !im, vis, bil, hid, !im, div, Div, li,, fie, in)
/** * 匹配多个字符串中的某一些 * [] : 把需要匹配的字符放到中括号里面,注意是区分大小写 */ val p6 = """[bd]i.""".r
println(p6 findAllIn a2 toList) //List(div, bil, div)
/** * 匹配一个范围中的某一些 * - : 连接符(-)左边是起始,右边是结束。特别注意 右边必须大于左边 * * [0-9]:匹配从0到9的数字 等价于 \d * [a-z]:匹配从a到z的字符 * [A-Z]:匹配从A到Z的字符 * [A-z]:匹配从A到z的字符、符号等(内部还有其他一些出字母的符号) * [0-9A-Za-z]:匹配数字和字符 */ val p7 = """.h[0-3]""".r
println(p7 findAllIn a2 toList) //List( h1, h2, h3)
/** * 用非(^)匹配一些不要的字符 * ^ :元字符 ^ 表示非 */ val p8 = """.h[^0-2]""".r
println(p8 findAllIn a2 toList) //List( hi, h3, h4, h5, h6, th,)
//---------------------------使用元字符简写--------------------------------------------
/** * [0-9]:匹配从0到9的数字 等价于 \d * [^0-9]:匹配非从0到9的数字 等价于 \D * [a-zA-Z0-9_]:匹配数字、字符和下划线(_) 等价于 \w * [^a-zA-Z0-9_]:匹配非数字、字符和下划线(_) 等价于 \W (大写) * */ val p9 = """.h\d""".r
println(p9 findAllIn a2 toList) //List( h1, h2, h3, h4, h5, h6)
val p10 = """.h\D""".r
println(p10 findAllIn a2 toList) //List( hi, th,)
val p11 = """.h\w""".r
println(p11 findAllIn a2 toList) //List( hi, h1, h2, h3, h4, h5, h6)
val p12 = """.h\W""".r
println(p12 findAllIn a2 toList) //List(th,)
//---------------------------重复匹配--------------------------------------------
/** * 有的时候,我们可以用 + 来进行最少一个的多次重复匹配, */ val a3 = "p123,156p78,14w89ew10,pw156 ?+"
val p13 = """\d""".r
println(p13 findAllIn a3 toList) //List(1, 2, 3, 1, 5, 6, 7, 8, 1, 4, 8, 9, 1, 0, 1, 5, 6)
//使用 + 来进行 多次重复匹配
val p14 = """\d+""".r
println(p14 findAllIn a3 toList) //List(123, 156, 78, 14, 89, 10, 156)
val p15 = """\d+[a-z]""".r
println(p15 findAllIn a3 toList) //List(156p, 14w, 89e)
val p16 = """\d+[a-z]+""".r
println(p16 findAllIn a3 toList) //List(156p, 14w, 89ew)
/** * 有时候我们希望匹配到 0次的情况,那么 + 就达不到要求, * 我们可以使用 * 来进行匹配 0次或多次,就是 比 + 多一个 0 次的范围 */ val p17 = """\d*[a-z]+""".r
println(p17 findAllIn a3 toList) //List(p, 156p, 14w, 89ew, pw)
/** * ? :匹配0个或1个 */ val p18 = """\d?[a-z]+""".r
println(p18 findAllIn a3 toList) //List(p, 6p, 4w, 9ew, pw)
/** * {n} : 匹配指定个数 n */ val p19 = """\d{2}[a-z]+""".r
println(p19 findAllIn a3 toList) //List(56p, 14w, 89ew)
/** * {m,n} : 匹配指定的一个范围 m到n 个 包含 m,n */ val p20 = """\d{0,1}[a-z]+""".r
println(p20 findAllIn a3 toList) //List(p, 6p, 4w, 9ew, pw)
//---------------------------位置匹配--------------------------------------------
val a4 = "The cat scattered his food all over the room"
val p21 = """cat""".r
println(p21 findAllIn a4 toList) //List(cat, cat)
/** * \b :用来匹配一个单词开始或者结束 * \B :不匹配一个单词边界 */ val p22 = """\bcat\b""".r
println(p22 findAllIn a4 toList) //List(cat)
val p23 = """cat\b""".r
println(p23 findAllIn a4 toList) //List(cat)
val p24 = """\bcat""".r
println(p24 findAllIn a4 toList) //List(cat)
//---------------------------子表达式--------------------------------------------
/** * () :内部的内容就作为一个子式 常与 (){n}一起使用 */ val a5 = "my Ip is 192.168.58.10"
val p25 = """(\d{1,3}\.){3}\d{1,3}""".r
println(p25 findAllIn a5 toList) //List(192.168.58.10)
}
}
参考文献
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html?is-external=true
http://baike.baidu.com/link?url=ksRyhmGLTGNTVULyqQlyoSojcT8rsmoNQU09GkXT7fp7Neleml2NKHFM77sA_mDH3NeP_mUfDLoRF4W-Y9fkAq