Skip to content
Fr Simon's Secondhand Bookstall

Fr Simon's Secondhand Bookstall

Selling my books to make space (for more books)

  • Home
  • Bookstall
    • Books and other Stuff
    • Basket
    • Checkout
  • Refund and Returns Policy
  • Other things
    • Dance
    • Technology & Geekery
      • Fr Simon’s Unofficial Remote Controller
      • Easyworship Remote Control
      • get_iPlayer GUI Version 2
      • Electric Ordo (Calendar) 2023
      • jWorship
      • Development and Testing
      • NAS Drives Mapper
      • Development Privacy Policy
    • Liturgy & Worship
      • Funeral Materials
      • Common Worship RTF Files
      • Spiritual Materials
    • Blog
  • My Publications
    • MA Dissertation: Sacraments in Digital Space
  • About
  • Toggle search form

Regex to Extract Bible References from a Text using VBA

Posted on August 29, 2019 By spr No Comments on Regex to Extract Bible References from a Text using VBA

I must admit I don’t really understand the complexities of Regex, the text matching spells which manage to make sense whilst appearing like a line of gibberish. Thank heavens for sites like https://regex101.com/ which help idiots like me experiment until I can get what I need.

In this case, I have text strings which contain a bible reference and the reading all together, when I just want to extract the Bible Reference. The post at https://stackoverflow.com/questions/22254746/bible-verse-regex produced the spell I needed to create a Function in VBA for Microsoft Access to do what I needed.

First you need to enable the VBA Regex Reference, so at the top of the VBA Code Window in Access, select TOOLS/REFERENCES and then check Microsoft VBScript Regular Expressions 5.5 which will enable this function to be recognised.

This is the function:

Function regexReadings(sReading) As String
 ' need to enable Microsoft VBScipt Regular Expressions 5.5 in Tools/References
 ' Regex to extract Bible Verse https://stackoverflow.com/questions/22254746/bible-verse-regex
 ' (\d)\s([a-z]+)\s(\d+)(?::(\d+))?(\s-\s(\d+)(?:\s([a-z]+)\s*(\d+))?(?::(\d+))?)?

 Dim rExp As Object, rMatch As Object, r_item As Object, result As String
 Set rExp = CreateObject("vbscript.regexp")
 With rExp
     .Global = True
     .MultiLine = False
     .IgnoreCase = True
     .Pattern = "(\d)\s([a-z]+)\s(\d+)(?::(\d+))?(\s-\s(\d+)(?:\s([a-z]+)\s*(\d+))?(?::(\d+))?)?"
 End With
 Set rMatch = rExp.Execute(sReading)
 If rMatch.Count > 0 Then
     For Each r_item In rMatch
         result = result & r_item.Value & " "
     Next r_item
 End If
 regexReadings = result
 End Function

The key thing is the Regex spell:

(\d)\s([a-z]+)\s(\d+)(?::(\d+))?(\s-\s(\d+)(?:\s([a-z]+)\s*(\d+))?(?::(\d+))?)? 

I have only the vaguest understanding of how it works, but believe me, it does.

Therefore If I invoke

Debug.Print regexReadings("Luke 15:1Now the tax collectors and sinners were all gathering around to hear Jesus.")

It returns “Luke 15:1”

Use as you wish, credit to the author of the Regex Spell, Niet

parish

Post navigation

Previous Post: Batch Script Backup to Network NAS Drive
Next Post: Script to check if a Linux service is running…

Related Posts

Halloween – the Darkness runs from the Light parish
Archbishop’s Digital Charter – a short reflection parish
Vestry tidying at St Edward's parish
Marking of the Houses with Chalk at Epiphany parish
Isaiah 9: the writing on the wall parish
Greenbelt Liturgy alt.worship

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Copyright © 2023 Fr Simon's Secondhand Bookstall.

Powered by PressBook Grid Dark theme

FREE SHIPPING | CHEAPER THAN ABEBOOKS Dismiss