vba - Need to tweak Excel function that uses regex to extract IP addresses from job output -
i have job output reads this...
node number 1 (172.xxx.123.210): error: cannot download running config: connection refused 172.xxx.123.210
node number 2 (172.xxx.124.162): error: cannot download running config: connection refused 172.xxx.124.162
i want handle on ip addresses in brackets failing job. i've cobbled vb function contains regex extract ip's , working great. catch though, regex ^.*\((\d.*)\)
looking sequence of characters within brackets such as...
node number 1 (172.123.123.210):
and when call function on line doesn't contain brackets returns zero. haven't got reputation points allow me post image try if see output!
i don't want return 0 if there isn't ip in brackets i'd rather left cell empty. suggestions how can tweak function this?
here's function using...
function getip(info string) dim allmatches object dim re object set re = createobject("vbscript.regexp") re.pattern = "^.*\((\d.*)\)" re.global = true re.ignorecase = true set allmatches = re.execute(info) if (allmatches.count <> 0) result = allmatches.item(0).submatches.item(0) end if getip = result end function
i found using 2 regular expression patterns allows recognition of ip addresses or without brackets:
private sub commandbutton1_click() set regex = createobject("vbscript.regexp") set regex2 = createobject("vbscript.regexp") regex.global = true regex.ignorecase = true regex.pattern = "\(([0-9]{3}).([0-9]{3}).([0-9]{3}).([0-9]{3})\)" regex2.global = true regex2.ignorecase = true regex2.pattern = "([0-9]{3}).([0-9]{3}).([0-9]{3}).([0-9]{3})" = 1 usedrange.rows.count set allmatches = regex.execute(cells(i, 1).value) set othermatches = regex2.execute(cells(i, 1).value) if allmatches.count <> 0 cells(i, 1).interior.colorindex = 33 end if if othermatches.count <> 0 cells(i, 1).interior.colorindex = 33 end if next end sub
this loop through column 1, wrote ip addresses, , highlight valid ip addresses or without brackets , leave non-ip addresses blank.
i found when including both versions of regular expression or didn't work , i'm not sure why, maybe de-limiting brackets?
hope find useful.
Comments
Post a Comment