『壹』 python正則表達式匹配Ip地址和文件地址
『貳』 正則匹配ip
正則表達式獲取漢子、數字、字母的方法
'正則取漢字!
str = "山a里b有c322個和d尚一e次打f水 打了2233桶水"
Dim Rlt()
Dim regEx, Match, Matches, i
i = 0
Set regEx = New RegExp
regEx.IgnoreCase = True
Execute "regEx.Global = True"
regEx.pattern = "[u4E00-u9FA5]"
Set Matches = regEx.Execute(str)
ReDim Rlt(Matches.Count)
For Each Match in Matches
Rlt(i) = Match.Value
i = i + 1
Next
MsgBox Join(rlt,"")
'正則取數字!
str = "山a里b有c322個和d尚一e次打f水 打了2233桶水"
Set regEx = New RegExp
regEx.IgnoreCase = True
Execute "regEx.Global = True"
regEx.pattern = "d+"
Set Matches = regEx.Execute(str)
ReDim Rlt(Matches.Count)
For Each Match in Matches
Rlt(i) = Match.Value
i = i + 1
Next
MsgBox Join(rlt,"")
'正則取字母!
str = "山a里b有c322個和d尚一e次打f水 打了2233桶水"
Dim Rlt()
Dim regEx, Match, Matches, i
i = 0
Set regEx = New RegExp
regEx.IgnoreCase = True
Execute "regEx.Global = True"
regEx.pattern = "[a-zA-Z]"
Set Matches = regEx.Execute(str)
ReDim Rlt(Matches.Count)
For Each Match in Matches
Rlt(i) = Match.Value
i = i + 1
Next
MsgBox Join(rlt,"")
-------------------------------------
正則通用函數版(進階篇)
語法:返回值=正則(參數1, 參數2)
參數1:字元串,放入想過濾的字元串
參數2:整數,想過濾的模式,0為取漢字,1為取數字,2為取英文字母,3為取英文字母和數字,4為排除數字
另外可使用字元串模式
ex:"12345abcde" 代表只取數字12345跟英文小寫abcde
ex:"^12345abcde" 代表排除數字12345跟英文小寫abcde
如果想取符號的話,在想要取的符號前面加
ex:",.?:;" 代表取符號 ,.?:;
返回值:字元串,取出之後的結果
應用範例:
漢字=正則("a536小b4冉4cj1521博d11ja客Dg",0)
TracePrint 漢字 ' 返回=小冉博客
數字=正則("ranlingqi.com/wordpress500bug.html",1)
TracePrint 數字 ' 返回=500 //如果是500bug1.html 則返回 5001
字母=正則("ranlingqi.com/wordpress500bug.html",2)
TracePrint 字母 ' 返回=ranlingqicomwordpressbughtml
//---模式3,模式4就不舉例了,自己嘗試吧!~~~
取字元串=正則("a536小b4冉4cj1521博d11ja客Dg","a4b")
TracePrint 取字元串 ' 返回=ab44a
排除字元串=正則("a536小b4冉4cj1521博d11ja客Dg","^a45b36")
TracePrint 排除字元串 ' 返回=小冉cj1,21博d11j客Dg
Function 正則(字元串, 模式)
//--模式0=取漢字,模式1=取數字,模式2=取英文字母,模式3=取英文字母和數字,模式4=排除數字
//--模式為字元串=只取或排除該字元串內有的字元
Dim mode,TQstring
Select Case 模式
Case 0
mode="[u4E00-u9FA5]"
Case 1
mode="d+"
Case 2
mode = "[a-zA-Z]"
Case 3
mode = "[a-zA-Z0-9]"
Case 4
mode = "[^0-9]"
Case Else
mode = "[" & 模式 & "]"
End Select
TQstring = "Dim i" & vbCrLf
TQstring = TQstring & "Dim regEx, Match, Matches" & vbCrLf
TQstring = TQstring & "i = 0" & vbCrLf
TQstring = TQstring & "Set regEx = New RegExp " & vbCrLf
TQstring = TQstring & "regEx.IgnoreCase = True " & vbCrLf
TQstring = TQstring & "regEx.Global = True" & vbCrLf
TQstring = TQstring & "regEx.pattern ="""& mode &"""" & vbCrLf
TQstring = TQstring & "Set Matches = regEx.Execute("""& 字元串 &""")" & vbCrLf
TQstring = TQstring & "ReDim Rlt(Matches.Count)" & vbCrLf
TQstring = TQstring & "For Each Match in Matches " & vbCrLf
TQstring = TQstring & "Rlt(i) = Match.Value" & vbCrLf
TQstring = TQstring & " i = i + 1" & vbCrLf
TQstring = TQstring & "Next" & vbCrLf
TQstring = TQstring & "Set regEx = Nothing"
Execute TQstring
正則 = Join(Rlt, "")
End Function
小冉博客看到的 ranlingqi.com
『叄』 python中匹配ip的正則表達式 如果我要匹配192.168.*.*的網段呢 新手求教!!
importre
reg=re.compile(r"(?<![0-9.])((2[0-4][0-9]|25[0-5]|[01]?[0-9]{1,2}).){3}(2[0-4][0-9]|25[0-5]|[01]?[0-9]{1,2})(?![0-9.])")
(?<![0-9.])((2[0-4][0-9]|25[0-5]|[01]?[0-9]{1,2}).){3}(2[0-4][0-9]|25[0-5]|[01]?[0-9]{1,2})(?![0-9.])
從文本中匹配ip的正則表達式
importre(?<![d.])192.168(.(2[0-4]d|25[0-5]|[01]?d{1,2})){2}(?![d.])
reg=re.compile(r"(?<![d.])192.168(.(2[0-4]d|25[0-5]|[01]?d{1,2})){2}(?![d.])")
在文本中匹配 192.168網段的正則表達式
『肆』 如何使用正則表達式匹配IP地址
<%
'asp 的
Set reg = New RegExp
reg.Pattern = "^123.123(.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){2}$"
reg.IgnoreCase = True
reg.Global = True
response.Write(reg.test("123.123.25.25"))
%>
//javaScript 的
<script type="text/javascript">
var reg = /^123.123(.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){2}$/;
alert(reg.test("123.123.25.25"));
</script>
『伍』 Python正則表達式匹配IP地址的問題
表示非捕獲組,即只匹配這個組裡面的內容,而不會將這個組添加到group中,不佔用group的位置。
『陸』 python 正則表達式.*如何把ip地址提取出來
#!/usr/bin/envpython
#-*-coding:utf-8-*-
importre
html='<bodystyle="margin:0px"><center>您的IP是:[42.120.74.89]來自:浙江省杭州市阿里雲</center></body></html>'
reg=re.compile(r'[(d{1,3}.d{1,3}.d{1,3}.d{1,3})]')
item=re.findall(reg,html)
printitem[0]
『柒』 請教Python中匹配IP的正則表達式
下面是IPv4的IP正則匹配表達式
importre
#簡單的匹配給定的字元串是否是ip地址,下面的例子它不是IPv4的地址,但是它滿足正則表達式
ifre.match(r"^(?:[0-9]{1,3}.){3}[0-9]{1,3}$","272.168,1,1"):
print"IPvaild"
else:
print"IPinvaild"
#精確的匹配給定的字元串是否是IP地址
ifre.match(r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$","223.168.1.1"):
print"IPvaild"
else:
print"IPinvaild"
#簡單的從長文本中提取中提取ip地址
string_ip="isthis289.22.22.22ip?
result=re.findall(r"(?:[0-9]{1,3}.){3}[0-9]{1,3}",string_ip)
ifresult:
printresult
else:
print"recannotfindip"
#精確提取IP
result=re.findall(r"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)",string_ip):
ifresult:
printresult
else:
print"recannotfindip"
下面是IPv6的正則匹配表達式
string_IPv6="1050:0:0:0:5:600:300c:326b"
#匹配是否滿足IPv6格式要求,請注意例子里大小寫不敏感
ifre.match(r"^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$",string_IPv6,re.I):
print"IPv6vaild"
else:
print"IPv6invaild"
#提取IPv6,例子里大小寫不敏感
result=re.findall(r"(?<![:.w])(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}(?![:.w])",string_IPv6,re.I)
#列印提取結果
printresult
『捌』 正則表達式匹配ip地址並替換
sed不支持d的數字表達,另外要加-r參數:
『玖』 python匹配IP和埠
importre
results=re.findall('(?isu)<td>(d+).(d+).(d+).(d+)</td>s*<td>(d+)</td>',your_html)
forip,portinresults:
print'%s:%s'%(ip,port)