Sunday, January 21, 2018

Find Bible Verses using PowerShell

Using regular expressions and PowerShell we can create some code which will parse a Bible verse and then these values can be used in other ways.  In the function below I've used the .NET RegEx class to read Bible verses, which can be read from a text/CSV file into a variable.  It will find Bible verses within other text too.

function Get-BibleReferences {
    <#
        .SYNOPSIS
            Finds scripture references within the provided text.

        .DESCRIPTION
            Uses regular expressions to find references to the Bible and returns them as a set.

        .PARAMETER Text
            The text to parse for scripture references.

        .EXAMPLE
            PS C:\> Get-BibleReferences "Titus 3:1  Really any text can be here -- 1 Timothy 2:1-2 -" | Format-Table -AutoSize

            Reference       Book      Chapter Verses From To
            ---------       ----      ------- ------ ---- --
            Titus 3:1       Titus     3       1      1     
            1 Timothy 2:1-2 1 Timothy 2       1-2    1    2

        .NOTES
            Dag Calafell
            01.20.2018

        .LINK
            https://dynamicsax365trix.blogspot.com/
    #>
    [CmdletBinding(DefaultParameterSetName="Default")]
    param(
        [Parameter(ParameterSetName="Default",
            ValueFromPipelineByPropertyName=$true,
            Mandatory=$true,
            Position=0)]
        [ValidateNotNullOrEmpty()]
        [string[]]$text
    )

    $regex = new-object System.Text.RegularExpressions.Regex("(?(?:(?:[123]|I{1,3})\s*)?(?:[A-Z][a-zA-Z]+|Song of Songs|Song of Solomon)).?\s*(?1?[0-9]?[0-9]):\s*(?\d{1,3})(?:[,-]\s*(?\d{1,3}))*", [System.Text.RegularExpressions.RegexOptions]::MultiLine)
    $regexMatches = $regex.Matches($text)

    foreach ($match in $regexMatches)
    {
        $groups = $match.Groups
        $book         = $groups[1].Value
        $chapter      = $groups[2].Value
        $fromVerseNum = $groups[3].Value
        $toVerseNum   = $groups[4].Value

        $object = New-Object –TypeName PSObject
        $object | Add-Member –MemberType NoteProperty –Name Reference –Value $groups[0].Value
        $object | Add-Member –MemberType NoteProperty –Name Book –Value $book
        $object | Add-Member –MemberType NoteProperty –Name Chapter –Value $chapter

        if ($groups[4].Success)
        {
            $object | Add-Member –MemberType NoteProperty –Name Verses –Value ("{0}-{1}" -f $fromVerseNum, $toVerseNum)
            $object | Add-Member –MemberType NoteProperty –Name From –Value $fromVerseNum
            $object | Add-Member –MemberType NoteProperty –Name To –Value $toVerseNum
        }
        else
        {
            $object | Add-Member –MemberType NoteProperty –Name Verses –Value $fromVerseNum
            $object | Add-Member –MemberType NoteProperty –Name From –Value $fromVerseNum
            $object | Add-Member –MemberType NoteProperty –Name To –Value ""
        }

        # Return the info
        $object
    }
}

# Example
Get-BibleReferences "Titus 3:1  Really any text can be here -- 1 Timothy 2:1-2 -" | Format-Table -AutoSize

Output
Reference       Book      Chapter Verses From To
---------       ----      ------- ------ ---- --
Titus 3:1       Titus     3       1      1      
1 Timothy 2:1-2 1 Timothy 2       1-2    1    2 

This code is part of a larger script I'm working on to take a file of scripture references and return the full text to aid me in developing Bible studies.

Credit goes to RegexLib for the starting regex that I modified capture the data into groups.  Many times it is easier to find something to start with and extend it to what is desired.

No comments:

Post a Comment