Regular Expression – Tools

Background

Wanted to list a couple of products that one can use to quickly try our Regular Expressions against a text dataset.

Online Tools

Regular Expression Tester

  1. Dan’s Tools
    • Regex Testing
  2. gskinner
    • Regexr.com

 

In Depth

Dan’s Tools

Regex Testing

Outline

  1. Launch a web browser
  2. Sojourn to https://www.regextester.com/ ( Link )
  3. Paste Test String
  4. Paste Regular Expression
  5. Matches are auto-highlighted

 

Image

DanTools.RegularExpression.20190411.0920AM

 

gskinner

Regex Testing

Outline

  1. Launch a web browser
  2. Sojourn to https://www.regexr.com/ ( Link )
  3. Paste Test String
  4. Paste Regular Expression
  5. Matches are auto-highlighted

 

Image

 

regexr.com.20190411.0932PM

 

 

 

 

 

Powershell/Regular Expression :- Get Host’s IP Address

Background

Quick follow-up to our past attempts at using Regular Expression to parse data within a lithe PowerShell Script.

Regular Expression

Positive Look Behind

  1. Regular-Expressions.Info
    • Lookahead and Lookbehind Zero-Length Assertions
      • Web Link
        Link
      • The construct for positive lookbehind is (?<=text): a pair of parentheses, with the opening parenthesis followed by a question mark, “less than” symbol, and an equals sign.
    • Regular Expression Reference: Special Groups
      • Web Link
        Link
      • Matches at a position if the pattern inside the lookbehind can be matched ending at that position.
      • Insight
        regular-expressions.info.positive.lookbehind.20190411.0825AM

Code

Powershell

Outline

  1. Get results of running IPConfig
    • Run IPConfig
    • Capture output in local variable
  2. Prepare Regular Expression
    • Look for IP Address
      • IPv4 Address Only

Regular Expression


(?<=IPv4 Address(\. )*): (\w*).(\w*).(\w*).(\w*)

Actual Code


Set-StrictMode -version 2
 
[string] $regExpPattern=$null;
[string] $inputString=$null;
[int]    $iNumberofMatches=0;
[object] $objList =$null;
[object] $objRegEx =$null;
[string] $objType = $null;
[int]    $iElementID = 0;
[string] $log=$null;
[string] $regExpOption=$null;
[Boolean] $debug = $false;
#[Boolean] $debug = $true;

[string]$domainName = $null;
[array]$domainList = $null;
[int] $domainListNumberofEntries=0;
[string]$topLevelDomain = $null;
[string]$secondLevelDomain = $null;
    
        
# Regular Expression
# Look for IPV4, once found get periods
#   look for 4 digits
# (?<=IPv4 Address(. )*): (\w*).(\w*).(\w*).(\w*)
$regExpPattern= "(?<="
$regExpPattern+= "IPv4 Address" 
$regExpPattern+= "(\. )*";
$regExpPattern+= ")";

$regExpPattern+= ":";
$regExpPattern+= " ";
$regExpPattern+= "(\w*).(\w*).(\w*).(\w*)";

# Regular Expression Option
$regExpOption="IgnoreCase";

# Run IP Config
$inputString = ipconfig

if ($debug)
{

    Write-Host;
    Write-Host;

    $log = "Input is {0} " -f $inputString;

    Write-Host($log);
   
    $log = "==========================================================================="
    Write-Host($log);

    Write-Host;
    
    
}

$log = "[regex]::expression is {0} " -f $regExpPattern;
Write-Host($log);


$objList = ( [regex]::matches( `
                                  $inputString `
                                , $regExpPattern `
                                , $regExpOption`
                             ) `
            )
 
if ($objList)
{
 
    if ($debug)
    {
         
        $objType = $objList.GetType();
 
        $log = "[regex]::matches return type is {0} " -f $objType;
 
        Write-Host($log);
 
    }
 
     
    $iNumberofMatches = $objList.Count;
    
    if ($debug)
    {   
 
        $log = "Number of Matches is {0}" -f $iNumberofMatches;
 
        Write-Host($log);
        
    }       
 
    #$objList;
    
    if ($iNumberofMatches -gt 0)
    {
 
        ForEach ($obj in $objList)
        {

            $ipAddress = $obj.Value;
        
            $log = "IP Address is {0}"  -f $ipAddress;
         
            Write-Host($log);
        
        }   
    }
 
 
}
else
{

	Write-Host "==================="
	$inputString
	Write-Host "==================="

    Write-Host "$objList is null"
}

Output

regex.regAhead.20190411.0145PM

Powershell :- Regular Expression – Using [regex]::matches

Background

In our last post spoke about using the match operator to issue Regular Expression calls.

Lineage

  1. PowerShell :- Regular Expression
    Link

RegEx Class

The -Match operator is a bit restrictive in terms of flexibility and so it is likely better to utilize the RegEx Class.

Outline

  1. Access RegEx Class Match operator
    • The first operand is the text
    • The second operator is the pattern
    • And, the third is RegEx Options
      • In our case, ignorecase
  2. Result is sent back as a collection
    • Specifically System.Text.RegularExpressions.MatchCollection
    • Simple access using index suffixes for our case

Code



Set-StrictMode -version 2

[string] $regExpPattern=$null;
[string] $fullname=$null;
[int]    $iNumberofMatches=0;
[hashtable] $obListofNames =$null;
[object] $objRegEx =$null;
[string] $listofNamesType = $null;
[int]    $iElementID = 0;
[string] $log=$null;
[string] $regExpOption=$null;
[Boolean] $debug = $false;
#[Boolean] $debug = $true;

# Regular Expression
$regExpPattern="(\w+)(\w+)";

# Regular Expression Option
$regExpOption="IgnoreCase";

$fullname="Daniel Adeniji"


$objListofNames = ( [regex]::matches( `
                                          $fullname `
                                        , $regExpPattern `
                                        , $regExpOption`
                                        ) `
                  )

if ($objListofNames)
{

    if ($debug)
    {

        
        $log = "[regex]::expression is {0} " -f $regExpPattern;

        Write-Host($log);

        
        $listofNamesType = $objListofNames.GetType();

        $log = "[regex]::matches return type is {0} " -f $listofNamesType;

        Write-Host($log);

    }

    
    $iNumberofMatches = $objListofNames.Count;

    $log = "Number of Matches is {0}" -f $iNumberofMatches;

    Write-Host($log);

    if ($iNumberofMatches -gt 0)
    {

        $log = "First name is {0}" -f $objListofNames[0];

        Write-Host($log);

    }

    if ($iNumberofMatches -gt 1)
    {

        $log = "Last name is {0}" -f $objListofNames[1];

        Write-Host($log);

    }   

}
else
{
    Write-Host "$objListofNames is null"
}

Output

regex.matches.output.20190406.0935AM

References

  1. Microsoft
    • System.Text.RegularExpressions
      • Regex.Matches Method

PowerShell :- Regular Expression

 

Background

Looking at a useful tool written in PowerShell.

But, it was fading.

Time to play around with Regular Expression, Regex.

Code

Powershell

Objective

  1. Prepare Regular Expression
    • In our case, stored in $regexp
  2. Prepare Input
    • In our case, store in $fullname
  3. Issue RegEx call using -match operator
  4. Results is saved in Hashtable
    • The name of the system populated Hashtable is $hashtable
      • The 0th element is the passed in input
      • 1 is the first match
      • 2 is the second match
      • And, on and on

Implementation


Set-StrictMode -version 2

[string] $regexp=$null;
[string] $fullname=$null;
[int]    $iNumberofMatches=0;
[hashtable] $obListofNames =$null;

$regexp="(\w+) (\w+)";
$fullname="Daniel Adeniji"

$fullname -match $regexp  | Out-Null;

$obListofNames = $Matches;

$iNumberofMatches = $obListofNames.Count-1;

Write-Host("Number of matches :- " + $iNumberofMatches);

if ($obListofNames)
{
    Write-Host("Full Name :- " + $obListofNames[0]);
}

if ($iNumberofMatches -gt 0)
{
    Write-Host("First Name :- " + $obListofNames[1]);
}

if ($iNumberofMatches -gt 1)
{
    Write-Host("Last Name :- " + $Matches[2]);
}

Output

powershell.regex.20190405.0348PM