正則表達式
導航
1.初識正則表達式,語法
2.將搜索的字符串,以及要搜索的關鍵字,還有要修改的都變爲自己輸入
3.添加新屬性global
4.三個屬性三個方法 Execute(要搜索的字符串) 第三種方法:Backreferencing
5.matches集合,與上面Execute相配合使用,及各個屬性
————————————————————————————————————————
1.初識正則表達式,語法
dim re,s
set re = new RegExp
re.pattern = "like" '將搜索值設置爲like
s = "i like Linlin" '設置要搜索的字符串
msgbox re.replace(s,"love") '這裏後面的爲要修改的
運行結果:
————————————————————————————————————————
2.將搜索的字符串,以及要搜索的關鍵字,還有要修改的都變爲自己輸入
dim re,s,news
set re = new RegExp
s = inputbox("輸入完整的字符串:")
re.pattern = inputbox("輸入要搜索的關鍵詞:") '將搜索值設置爲like
news = inputbox("替換的新字符:")
msgbox re.replace(s,news) '這裏後面的爲要修改的
————————————————————————————————————————
3.如果我們要搜索的字符串中有多個關鍵詞,如你要搜索in,字符串中有in,mainly,pain
若你只想找出in,那麼就要在pattern中添加\b
dim re,s
set re = new RegExp
re.Pattern = "\bin"
'下面這個沒有也就相當於re.global = false
re.global = true '這個屬性true代表搜索全部字符串,false就只是搜索到第一次位置則結束
s = "the rain in Spain falls mainly on the pains"
msgbox re.replace(s,"in the country of")
global:這個屬性true代表搜索全部字符串,false就只是搜索到第一次位置則結束
————————————————————————————————————————
4.三個屬性三個方法
dim re,s
set re = new RegExp
re.Pattern = "\bIn" '也就是搜索的字符串 (模式)
'下面這個沒有也就相當於re.global = false
re.global = true 'true 是整體字符串搜索 false 就是整個字符串找到一次就結束了 (全部的)
re.IgnoreCase = true 'true 是不分大小寫 false 要分大小寫 (忽略不計)
s = "the rain in Spain falls mainly on the pains"
msgbox re.replace(s,"in the country of")
set re = nothing
正則表達式字符表
例子1:(搜索字符串中的數字,進行更改)
dim re,s
set re = new RegExp
re.pattern = "[123456]" '搜索指定範圍內的 或者也可以寫[1-9]都是差不多意思
s = "there is 1 books"
msgbox re.replace(s,"many")
查找非數字有三種方式:
re.pattern = “[^,0-9]”
re.pattern = “[\D]”
re.pattern = " [^\d]"
查找數字個數可以這樣:
re.pattern = “\d{3}”
re.pattern = “\d+” '尋找一次或者多次數字 \d* 尋找0次或者多次數字 \d?尋找零次或者1次
————————————————————————————————————————
正則表達式的第二個方法:Execute(要搜索的字符串)
re.execute(s) //獲取集合
re.execute(s).count //獲取集合中的個數
for each match in colmathes msgbox "Found valid URL: " & match.val //獲取集合中的對應值
看這個小例子:()
dim re,s,colmathes,match
set re = new RegExp '創建正則表達式對象
re.global = true '可以搜索這個字符串
re.pattern = "http://(\w+[\w-]*\w+\.)*\w+" '搜索的關鍵字
s = "http://www.kingsley-hughes.com is a valid web address. And so is "
s = s & vbCrlf & "http://www.wrox.com, And"
s = s & vbcrld & "http://www.pc.ibm.com - even with 4 levels "
set colmathes = re.execute(s) 'Execute(s) 返回的是一個集合 ,所以要創建對象
if re.test(s) then '判斷是否存在其模式
msgbox "找到相應" & re.Execute(s).count & "個網址"
for each match in colmathes '用for each來遍歷集合,打印出集合中的值
msgbox "Found valid URL: " & match.value
next
else
msgbox "沒有找到相匹配的"
end if
運行結果:
————————————————————————————————————————
正則表達式第三種方法:Backreferencing
一個被記住的匹配結果就是模式的一部分
看例子:
dim re,s
set re = new RegExp
re.pattern = "(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)" '()表示去獲取到的對應值
re.global = true
s = "vbs is worth to learn"
msgbox re.replace(s,"$1 $2 $4 $5") '用$1 後面跟序號來表示在pattern中用括號來表示的,並且根據對應位置來打印
運行結果:
會根據對應想要輸出的位置來輸出
————————————————————————————————————————
5.matches集合,與上面Execute相配合使用,及各個屬性
訪問集合中的數:
set matchs = re.Execute(s)
msgbox matchs.item(0) '從集合中的第一個開始下標爲0
msgbox matchs.item(1)
msgbox matchs.item(2)
屬於matches集合中的各個屬性,firstindex,length,value(第一個字符串編號,長度,值)
dim re,s,matchs,jihe
set re = new RegExp
re.pattern = "http://(\w+[\w-]*\w+\.)*\w+"
re.global = true '搜索範圍爲整個字符串
s = "http://www.kingsley-hughes.com is a valid web address. And so is "
s = s & vbCrlf & "http://www.wrox.com, And"
s = s & vbcrld & "http://www.pc.ibm.com - even with 4 levels "
set matchs = re.Execute(s) '獲取集合
for each jihe in matchs '獲取集合中的對象
msgbox jihe.firstindex & " " & " " & jihe.length & jihe,value '獲取到字符串第一個編號,以及整個字符串的長度,和這個字符串輸出
next
————————————————————————————————————————
7.小案例
1.測試輸入是否爲美國電話
'驗證美國電話號碼
option explicit
dim re,s
set re = new RegExp
re.pattern = "\([\d]{3}\)[0-9]{3}-[\d]{4}"
s = inputbox("請輸入您的號碼:")
if re.test(s) then
msgbox "輸入成功"
else
msgbox "輸入格式不正確"
end if
2.分解url
'分解url 分解對應的協議,域名地址,頁面,路徑
option explicit
dim re,s
set re = new RegExp
re.pattern = "(\w+):\/\/([^/:]+)(:\d*)?([^$]*)"
re.global = true
re.ignorecase = true
s = "http://www.wrox.com:80/misc~pages/support.shtml"
msgbox re.replace(s,"$1")
msgbox re.replace(s,"$2")
msgbox re.replace(s,"$3")
msgbox re.replace(s,"$4")
解析:
會將http://www.wrox.com:80/misc~pages/support.shtml分成四個部分
第一部分:(\w+) ——http
第二部分:([^/:]+)——www.wrox.com
第三部分:(:\d*)——:80
第四部分:([^$]*)——/misc~pages/support.shtml
\w就是匹配字母數字以及下劃線,相當於[0-9a-zA-Z_],後面跟着+說明前面爲1個或者多個
[^/:]我們拆開來看,/ : 爲轉義字符: ,那麼 ^ / :就是除了: 的字符,[]括起來就是查找包含除了:的字符,就會匹配到:結束
\d*:就是匹配0個或者多個數字
^$: $是末尾的意思那麼,[ ^ $ ]* 是除了末尾符號匹配0個或者多個
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3.檢查html的元素
'檢查html元素
option explicit
dim re,s
set re = new RegExp
re.pattern = "<(.*)>.*<\/\1>"
re.global = true
re.ignorecase = true
s = "<p>this is a paragraph</p>"
if re.test(s) then
msgbox "found"
else
msgbox "not found"
end if
set re = nothing
運行結果:found
解析:
.*:.就是代表除了換行符之外任意的單個字符
\1:就是替換上一次圓括號的內容
————————————————————————————————————————
4.匹配空白
'匹配空白
dim re,s
set re = new RegExp
re.pattern = "^[ \t]*$" '匹配空白
re.global = true
s = " "
smsg = ""
set colmatchs = re.Execute(s)
for each objmatch in colmatchs
smsg = smsg & "Blank line found position " & _
objmatch.firstindex
next
msgbox smsg
————————————————————————————————————————
5.匹配HTML註釋標籤
'匹配空白
dim re,s
set re = new RegExp
re.pattern = "^.*<!--.*-->.*$"
s = " <title>A Title</title> <!-- a title tag -->"
if re.test(s) then
msgbox "found"
else
msgbox "not found "
end if
msgbox re.replace(s,"$1" & "$3") '可以將腳本中的註釋清除