IPV6 RegExMatch not working

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ddougal
Posts: 52
Joined: 09 Nov 2015, 14:26

IPV6 RegExMatch not working

27 Mar 2016, 11:06

How can I get this script to Match IPV6 addresses within a string. Seems to only work if the string only has an IPv6 address and nothing else.

Code: Select all

;s := "2001:558:4000:4b:4a1d:70ff:fe67:cccb"   ;works
s := "Interface   (DS-US)  192.168.0.7  DOC  S/C/CH-S/CG/CH    Mac   Bonded State       SIS      Qos      CPE  MAC address     IP Address   2001:568:4030:4b:4a1d:70ff:fe67:cccb   11/4/4-1/4/2      13     20x4  Operational 3.0 "   ;fails

If (is_ipv6(s))
	MsgBox, is ipv6
Else MsgBox, NOT ipv6

is_ipv6(a) {
	StringReplace, a, a, :, :, UseErrorLevel
	msgbox %ErrorLevel%
	If (!l := InStr(a, ":") == 1)
		r = :
	Loop, % l + 8 - ErrorLevel
		r = %r%0:
	StringReplace, a, a, ::, %r%
	Return, !!RegExMatch(a, "i)^(\[)?(?:[a-f\d]{1,4}:){7}[a-f\d]{1,4}(?(1)\]\d{1,5}|)$")

}
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: IPV6 RegExMatch not working

27 Mar 2016, 11:49

How about something like this.

Code: Select all

is_ipv6(a) {
	Return RegExMatch(a, "(.*:){7,7}")
}
Please excuse my spelling I am dyslexic.
ddougal
Posts: 52
Joined: 09 Nov 2015, 14:26

Re: IPV6 RegExMatch not working

27 Mar 2016, 13:09

That works great. How can I return or display the IP found in the string or if I can just get the position of where the address is in the string like FoundPos := RegExMatch(a, "(.*:){7,7}")
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: IPV6 RegExMatch not working

27 Mar 2016, 13:16

ddougal wrote:That works great. How can I return or display the IP found in the string or if I can just get the position of where the address is in the string like FoundPos := RegExMatch(a, "(.*:){7,7}")
If you wright it like this the ip will be stored in "ip".

Code: Select all

RegExMatch(a, "([a-zA-Z0-9]*:){7,7}[a-zA-Z0-9]*", ip)
Edit: It seems there is a problem because ":0000:0000:" can be represented as "::", this is not supported, and you can probably change "[a-zA-Z0-9]" to "[a-fA-F0-9]"
Edit 2: This is a little better "([a-fA-F0-9]{0,4}:){7,7}[a-fA-F0-9]{0,4}"
Edit 3: This catches all ipv6 addresses "([a-fA-F0-9]{0,4}:){2,7}[a-fA-F0-9]{0,4}" but it also has a greater likelihood of generating false positives.
Please excuse my spelling I am dyslexic.
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: IPV6 RegExMatch not working

28 Mar 2016, 09:22

If you're not looking for much in the way of explanation:

Code: Select all

match :=	"(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|"
 . "([0-9a-fA-F]{1,4}:){1,7}:|"
 . "([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|"
 . "([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|"
 . "([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|"
 . "([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|"
 . "([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|"
 . "[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|"
 . ":((:[0-9a-fA-F]{1,4}){1,7}|:)|"
 . "fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|"
 . "::(ffff(:0{1,4}){0,1}:){0,1}"
 . "((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}"
 . "(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|"
 . "([0-9a-fA-F]{1,4}:){1,4}:"
 . "((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}"
 . "(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))"
 , s :=	"Interface   (DS-US)  192.168.0.7  DOC  S/C/CH-S/CG/CH    Mac   Bonded State       SIS      Qos      CPE  MAC address     IP Address   2001:568:4030:4b:4a1d:70ff:fe67:cccb   11/4/4-1/4/2      13     20x4  Operational 3.0 "
if	RegExMatch(s,match,m)
	MsgBox %	m

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder, sachalamp and 151 guests