Difference between revisions of "Regular Expressions"

From vwiki
Jump to navigation Jump to search
(Re-arranged page)
(Reworked page)
Line 1: Line 1:
== Examples ==
=== Basic ===
{|cellpadding="2" cellspacing="0" border="1"
|- style="background-color:#bbddff;"
! Matches                  !!  Expression
|-
| Anything containing ''text''
| <code><nowiki> .*text* </nowiki></code>
|}


== Random ==
== Random ==
* '''Filename'''
* '''Filename'''
** <code> \d{4}-[A-Za-z]{3}-Week\d{1}.log </code>
** Example matches...
*** 2010-Feb-Week4.log
*** 2009-Dec-Week2.log
*** 1234-aBc-Week0.log


== Useful RegEx ==
 
== Useful/Standard RegEx ==
{|cellpadding="2" cellspacing="0" border="1"  
{|cellpadding="2" cellspacing="0" border="1"  
|- style="background-color:#bbddff;"
|- style="background-color:#bbddff;"
Line 31: Line 18:
| <code><nowiki> \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b </nowiki></code>
| <code><nowiki> \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b </nowiki></code>
|}
|}
== Examples ==
=== Logfile Name ===
''' <code> \d{4}-[A-Za-z]{3}-Week\d{1}.log </code> '''
Example matches...
* <code> 2010-Feb-Week4.log </code>
* <code> 2009-Dec-Week2.log </code>
* <code> 1234-aBc-Week0.log </code>
=== Between Parentheses ===
''' <code> (?<=\[)(.*?)(?=\]) </code> '''
Matches everything between <code> [ </code> and <code> ] </code>, so for example...
* <code> VMFS_SCSI_DS_01 </code> is matched from <code> [VMFS_SCSI_DS_01] My_VM/MyVM.vmdk
Its essentially done via three chunks of the regex...
# <code> (?<=\[) </code>
#* Requires that <code> [ </code> immediately proceeds the match
# <code> (.*?) </code>
#* Matches everything
# <code> (?=\]) </code>
#* Requires that <code> [ </code> immediately follows the match

Revision as of 15:24, 3 November 2010

Random

  • Filename


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