Hello, i am using the code below to select and display bible verses from a txt. bible file. The code is working, however when i select for example Genesis 1:2 the code returns other verses that begin with two for example 1:20, 21, 22. when i try to apply the \b (word boundary) then the code does not return any data
below is the code am using. any help will be greatly appreciated
textBox4.Text = "";
int a = System.Convert.ToInt32(txtch.Text);
int b = System.Convert.ToInt32(txtvs.Text);
string bibleFilePath = "C:/TextSpeech/Bible.txt";
string book = txtbook.Text;
int chapter = a;
int verse = b;
string searchFor = string.Format(@"{0} {1}:{2}", book, chapter,verse);
string [] lines = File.ReadAllLines(bibleFilePath,System.Text.Encoding.UTF8);
foreach (string line in lines)
{
if (line.StartsWith(searchFor))
{
textBox4.AppendText(line + Environment.NewLine);
}
}


Amit MohantyRank #112 · 20K reputationPosted Sep 23, 2024, 3:04 AM EDT
You can use regular expressions to enforce an exact match for the verse reference, including word boundaries. For example:
using System.IO; using System.Text.RegularExpressions; textBox4.Text = ""; int a = System.Convert.ToInt32(txtch.Text); int b = System.Convert.ToInt32(txtvs.Text); string bibleFilePath = "C:/TextSpeech/Bible.txt"; string book = txtbook.Text; int chapter = a; int verse = b; string searchFor = $@"\b{book} {chapter}:{verse}\b"; string[] lines = File.ReadAllLines(bibleFilePath, System.Text.Encoding.UTF8); Regex regex = new Regex(searchFor); foreach (string line in lines) { if (regex.IsMatch(line)) { textBox4.AppendText(line + Environment.NewLine); } }Marvin kakuruRank #3,979 · 374 reputationPosted Sep 23, 2024, 3:37 AM EDT
Thanks Amit, i have tried your method but code is still not return any data.
Jignesh KumarRank #91 · 22.9K reputationPosted Sep 23, 2024, 3:40 AM EDT
Hello Marvin,
You can achieve this using pattern matching,
using System; using System.IO; using System.Text.RegularExpressions; class Program { static void Main() { string book = "Genesis"; int chapter = 1; int verse = 2; string pattern = $@"^{book}\s{chapter}:{verse}\b"; // Read the Bible text file (replace the path with your actual file path) string[] bibleLines = File.ReadAllLines("path/to/bible.txt"); // print the Bible text file and search for a match foreach (var line in bibleLines) { if (Regex.IsMatch(line, pattern)) { Console.WriteLine(line); } } } }Aman GuptaRank #193 · 12.4K reputationPosted Sep 24, 2024, 2:36 AM EDT
Hi Marvin,
The issue arises because the StartsWith method is matching any verse that starts with Genesis 1:2, which can also include Genesis 1:20, Genesis 1:21, and similar entries.
To make sure you only match the exact verse, you can use a regular expression with a word boundary (\b) to ensure the search pattern matches exactly, including verse numbers like "Genesis 1:2" but not "Genesis 1:20". Here's how you can update your code:
Modified Code:
using System.IO; using System.Text.RegularExpressions; textBox4.Text = ""; int a = System.Convert.ToInt32(txtch.Text); int b = System.Convert.ToInt32(txtvs.Text); string bibleFilePath = "C:/TextSpeech/Bible.txt"; string book = txtbook.Text; int chapter = a; int verse = b; string searchFor = string.Format(@"{0} {1}:{2}\b", book, chapter, verse); // Add word boundary string[] lines = File.ReadAllLines(bibleFilePath, System.Text.Encoding.UTF8); // Use a regular expression to match exact verse format Regex regex = new Regex(@"^\b" + Regex.Escape(searchFor) + @"\b"); foreach (string line in lines) { if (regex.IsMatch(line)) { textBox4.AppendText(line + Environment.NewLine); } }With this approach, you should get only the exact verse you are searching for, like "Genesis 1:2", without the additional matches for other verses starting with "1:2".