TCL RegExp IP exceptions -


i have following tcl regexp extract exact ip line:

set ip [regexp -all -inline {((([2][5][0-5]|([2][0-4]|[1][0-9]|[0-9])?[0-9])\.){3})([2][5][0-5]|([2][0-4]|[1][0-9]|[0-9])?[0-9])} $ip_text] 

i'm using analyze log file, , works fine, except it's extracting domain name ip portion when domain name contains ip format (but in reverse), don't wan't

eg when ip_text = log file 61.140.142.192 - 2012-06-16, 192.142.140.61.broad.gz.gd.dynamic.163data.com.cn, chn, 1

i 61.140.142.192 & 192.142.140.61 61.140.142.192 legit.

and when ip_text = entry "61.140.170.118" resolved 118.170.140.61.broad.gz.gd.dynamic.163data.com.cn, , 61.140.185.45 verified.

i 61.140.170.118, 118.170.140.61 & 164.111.111.34 61.140.170.118 & 61.140.185.45 legit.

is there way make regexpr exclude ip's have domain name character after it? ie exclude <ip><dot> or <ip><dash> or <ip><any alpha/numeric character>

you can use negative lookahead constraint on end of re. written (?!\.|\d) in case, matches when next character not . or digit (it matches @ end of string, when there's no next character @ all). complicated regular expressions it's easier save them in variable (often global) since lets name re.

set ipaddrre {(((25[0-5]|(2[0-4]|1[0-9]|[1-9])?[0-9])\.){3})(25[0-5]|(2[0-4]|1[0-9]|[1-9])?[0-9])(?!\.|\d)} set ip [regexp -all -inline $ipaddrre $ip_text] 

the reason need prevent follower being digit? without that, re can stop matching 1 character earlier, allowing pick 192.142.140.6 out of sample text value want.


you should consider using non-capturing grouping task. replacing (…) (?:…) allow re engine use more efficient matcher internally. on lot of text, make substantial difference. example, version:

set ipaddrre {(?:(?:25[0-5]|(?:2[0-4]|1[0-9]|[1-9])?[0-9])\.){3}(?:25[0-5]|(?:2[0-4]|1[0-9]|[1-9])?[0-9])(?!\.|\d)} 

i see time execute half version listed in first part of answer (and 40% of original version required). however, produces different results — none of bits don't require — you'll need adapt other code too:

% set ip [regexp -all -inline $ipaddrre $ip_text] 61.140.142.192 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -