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);
}
}

Aman GuptaPosted Sep 24, 2024, 6:36 AM
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:
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".
Jignesh KumarPosted Sep 23, 2024, 7:40 AM
Hello Marvin,
You can achieve this using pattern matching,
Marvin kakuruPosted Sep 23, 2024, 7:37 AM
Thanks Amit, i have tried your method but code is still not return any data.
Amit MohantyPosted Sep 23, 2024, 7:04 AM
You can use regular expressions to enforce an exact match for the verse reference, including word boundaries. For example: