Regex Examples: Difference between revisions
m (→Between Parentheses: Fixed typo) |
(→Useful/Standard RegEx: Added CIDR) |
||
Line 6: | Line 6: | ||
! Matches !! Expression | ! Matches !! Expression | ||
|- | |- | ||
| ''' | | '''IPv4 Address''' | ||
| <code><nowiki> | | <code><nowiki> \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 </nowiki></code> | ||
|- | |||
| '''IPv4 Address''' alternative | |||
| <code><nowiki> \b(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b </nowiki></code> | |||
|- | |||
| '''IPv4 CIDR Address''' | |||
| <code><nowiki> \b(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/([1-9]|1[0-9]|2[0-9]|3[1-2])\b </nowiki></code> | |||
|- | |- | ||
| '''Hostname''' (no domain) | | '''Hostname''' (no domain) |
Latest revision as of 13:55, 27 September 2024
See the Regular Expressions page for more detail on syntax.
Useful/Standard RegEx
Matches | Expression |
---|---|
IPv4 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
|
IPv4 Address alternative | \b(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b
|
IPv4 CIDR Address | \b(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/([1-9]|1[0-9]|2[0-9]|3[1-2])\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.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...
(?<=\[)
- 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...
13
is 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
vRanger
at 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.3
fromScript-v1.2.3.ps1
Stepping through the regex...
(?<=v)
- Requires that
v
immediately precedes the match
- Requires that
[0-9]+
- Matches one or more digits
(\.[0-9]+)+
- Matches
.n
one 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