Wikia

Vim Tips Wiki

Changes: Match valid IP address

Edit

Back to page

(formatting and some rewording)
(Comments)
 
Line 25: Line 25:
   
 
==Comments==
 
==Comments==
  +
  +
For a detailed explanation about how this works and for a similar regex for IPv6 addresses, refer to this article: [http://regex-for.com/ip-addresses/ Regex for IP]

Latest revision as of 18:48, November 30, 2012

Tip 1073 Printable Monobook Previous Next

created 2005 · complexity basic · author Matous Jan Fialka · version 6.0


When editing a file containing IP addresses, it may be helpful to highlight the addresses in a special color. Adding the following code to vimrc will highlight instances of IP addresses in the file being edited.

syn match ipaddr /\(\(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]\?\)/
hi link ipaddr Identifier

This will highlight IP addresses the same way identifiers are highlighted in source code files. The following regular expression is used to find each address to be highlighted.

((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]?)

Other regular expressions can be used to find and highlight other patterns within text.

CommentsEdit

For a detailed explanation about how this works and for a similar regex for IPv6 addresses, refer to this article: Regex for IP

Around Wikia's network

Random Wiki