Regex Examples: Difference between revisions
(Initial creation - content from Regular Expressions page) |
(No difference)
|
Revision as of 13:33, 13 April 2012
See the Regular Expressions page for more detail on syntax.
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
|
| Multiple whitespace | \b {2,}\b
|
Detailed Examples
Logfile Name
\d{4}-[A-Za-z]{3}-Week\d{1}.log
Example matches...
2010-Feb-Week4.log2009-Dec-Week2.log1234-aBc-Week0.log
Between Parentheses
(?<=\[)(.*?)(?=\])
Matches everything between [ and ], so for example...
VMFS_SCSI_DS_01is matched from[VMFS_SCSI_DS_01] My_VM/MyVM.vmdk
Its essentially done via three chunks of the regex...
(?<=\[)- Requires that
[immediately proceeds the match
- Requires that
(.*?)- Matches everything
(?=\])- Requires that
[immediately follows the match
- Requires that
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...
13is matched fromvmhba3:0:13
Stepping through the regex...
(?<=:)- Requires that
:immediately proceeds the match
- Requires that
([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])- Matches any number between 0 and 255
$- Ensures that the proceeding match occurs at the end of a line
vRanger Backup text in VM Notes
\s?\bvRanger.*Repository \[.*\]\s?
Finds the text created by vRanger in a VM's notes (so you can strip it out)...
- EG
vRanger Pro Backup: Type [Full] Result [Success] Time [27/09/2010 06:46:54] Repository [VC_Server]
Stepping through the regex...
\s?- Matches any white-space at the start of the match
\bvRanger.*- Matches
vRangerat the start of a word and anything after until...
- Matches
Repository \[.*\]- Matches the end of a vRanger text segment,
Repository [hostname]
- Matches the end of a vRanger text segment,
\s?- Matches any white-space at the end of the match
Script Version
(?<=v)[0-9]+(\.[0-9]+)+(?=.)
Gets the script version number off the end of a script name
- EG Get
1.2.3fromScript-v1.2.3.ps1
Stepping through the regex...
(?<=v)- Requires that
vimmediately precedes the match
- Requires that
[0-9]+- Matches one or more digits
(\.[0-9]+)+- Matches
.none or more times
- Matches
(?=.)- Requires that
.follows the match
- Requires that
Lab Manager VM Prefix
(\b\d{6}-)
Matches the numeric prefix at the start of a Lab Manager VM name
- EG Matches
002310-from002310-Server-A-10