Regular Expressions

From vwiki
Revision as of 11:48, 12 November 2010 by Sstrutt (talk | contribs) (→‎Examples: Added "VMHBA LUN ID")
Jump to navigation Jump to search

Useful/Standard RegEx

Matches Expression
IP Address ^\b((25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)\.){3}(25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)\b
Hostname (no domain) \A(\w|-)+
Email address \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Examples

Logfile Name

\d{4}-[A-Za-z]{3}-Week\d{1}.log

Example matches...

  • 2010-Feb-Week4.log
  • 2009-Dec-Week2.log
  • 1234-aBc-Week0.log

Between Parentheses

(?<=\[)(.*?)(?=\])

Matches everything between [ and ], so for example...

  • VMFS_SCSI_DS_01 is matched from [VMFS_SCSI_DS_01] My_VM/MyVM.vmdk

Its essentially done via three chunks of the regex...

  1. (?<=\[)
    • Requires that [ immediately proceeds the match
  2. (.*?)
    • Matches everything
  3. (?=\])
    • Requires that [ immediately follows the match

VMHBA LUN ID

(?<=:)([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$

Finds a number between 0 and 255 immediately after a : at the end of a line, specifically intended to get the LUN ID from a VMware canonical path, so for example...

  • 13 is matched from vmhba3:0:13

Stepping through the regex...

  1. (?<=:)
    • Requires that  : immediately proceeds the match
  2. ([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])
    • Matches any number between 0 and 255
  3. $
    • Ensures that the proceeding match occurs at the end of a line