User Tools

Site Tools


Regular Expressions

Using regular expressions will help you do some advanced querying and data manipulation. I've mostly used them in PostgreSQL.

I don't have any examples of code, but I have an uncompleted Javascript project where I use them.

Here's some code that is a regular expression that will do forward and reverse lookups for “Book Chapter Verse(s)” ranges. This is Javascript here, but the patterns themselves won't change.

The context here is not related to the databases, but to examine a source of text data and find the strings that match.

var p_book_titles = "Genesis|Exodus|Leviticus|Numbers|Deuteronomy|Joshua|Judges|Ruth|1 Samuel|2 Samuel|1 Kings|2 Kings|1 Chronicles|2 Chronicles|Ezra|Nehemiah|Esther|Job|Psalms|Proverbs|Ecclesiastes|Solomon's Song|Isaiah|Jeremiah|Lamentations|Ezekiel|Daniel|Hosea|Joel|Amos|Obadiah|Jonah|Micah|Nahum|Habakkuk|Zephaniah|Haggai|Zechariah|Malachi|Matthew|Mark|Luke|John|Acts|Romans|1 Corinthians|2 Corinthians|Galatians|Ephesians|Philippians|Colossians|1 Thessalonians|2 Thessalonians|1 Timothy|2 Timothy|Titus|Philemon|Hebrews|James|1 Peter|2 Peter|1 John|2 John|3 John|Jude|Revelation|1 Nephi|2 Nephi|Jacob|Enos|Jarom|Omni|Words of Mormon|Mosiah|Alma|Helaman|3 Nephi|4 Nephi|Mormon|Ether|Moroni|Doctrine and Covenants|Moses|Abraham|Joseph Smith--Matthew|Joseph Smith--History|Articles of Faith";

var p_book_short_titles = "Gen.|Ex.|Lev.|Num.|Deut.|Josh.|Judg.|Ruth|1 Sam.|2 Sam.|1 Kgs.|2 Kgs.|1 Chr.|2 Chr.|Ezra|Neh.|Esth.|Job|Ps.|Prov.|Eccl.|Song.|Isa.|Jer.|Lam.|Ezek.|Dan.|Hosea|Joel|Amos|Obad.|Jonah|Micah|Nahum|Hab.|Zeph.|Hag.|Zech.|Mal.|Matt.|Mark|Luke|John|Acts|Rom.|1 Cor.|2 Cor.|Gal.|Eph.|Philip.|Col.|1 Thes.|2 Thes.|1 Tim.|2 Tim.|Titus|Philem.|Heb.|James|1 Pet.|2 Pet.|1 Jn.|2 Jn.|3 Jn.|Jude|Rev.|1 Ne.|2 Ne.|Jacob|Enos|Jarom|Omni|W of M|Mosiah|Alma|Hel.|3 Ne.|4 Ne.|Morm.|Ether|Moro.|D&C|Moses|Abr.|JS-M|JS-H|A of F";

// Find all the range strings
var pattern = "";

// pattern = pattern + "(Genesis|Leviticus) *";
pattern = pattern + "(" + p_book_titles + "|" + p_book_short_titles + ") *";

// Match Book c:v which is *always* the initial string
var chapter_verse = "(\\d+:\\d+)";
pattern = pattern + chapter_verse;

// Match possible Book c:v-v, c:v-v,v, c:v,v-v,v
var following_verses = "(((( ?- ?|, ?)?\\d+)*(?!:))*)?";
pattern = pattern + following_verses;

// Start all over again
pattern = pattern + "(( ?(-|,|;) ?)?";
pattern = pattern + chapter_verse + following_verses + ")*";

var re = new RegExp(pattern, 'g');
regular_expressions.txt · Last modified: 2019/03/24 16:26 by steve