How to Find End of a String Using Regular Expression in C#
The end of a text document often contains the summary or gist of the text. Therefore, extracting text from the end of a document is an important task for various natural language processing applications. In this article, you will study how to search for a substring i.e. a word or a sentence at the end of a string. You will see how to extract the end of a string and how to check if a sentence or a word occurs at the end of a string.
Searching the End of a String for Existence of Text
To see if a string or a particular word occurs at the end of a string, you can use the Match() function from regex expression. You have to pass the expression “string_to_search$” to the Regex class instructor.
The expression “string_to_search” tells the compiler to search for the specified word at the end of the sentence. If the string is found at the end of a string, the success attribute of the object returned by the Match() function returns true as well.
The following script searches for the word “world” at the end of a text string. Since the input string contains the word “world” at the end, the Match() function will return true and you will see a message the word is found in the string.
using System; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "Lions are one of the biggest cats in the world"; Regex myRegex = new Regex(@"world$"); var results = myRegex.Match(textFile); if (results.Success) Console.WriteLine("Word found at the end"); else Console.WriteLine("Word not found at the end"); } } }
Extracting a Word from the End of String
You can also extract the last word from a string using the Split() function from the Regex class and the expression “\s+”. This expression “\s+” when passed to the Split() function, returns a list of strings delimited via empty spaces. The last item in the list corresponds to the word at the end of the string. The last item in the string can be retrieved by passing a number one less than the split string length as the index number to the string.
Look at the following example for reference. Here the word “world” which is the last word in the string, will be returned.
using System; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "Lions are one of the biggest cats in the world"; Regex myRegex = new Regex(@"\s+"); var results = myRegex.Split(textFile); Console.WriteLine("The word at the end of the string is: " + results[results.Length - 1]); } } }
Extracting a Sentence from the End of String
You can find the sentence at the end of a string using the Split() method from the Regex class. You can use the expression : [;,.?!]. The expression, when used with the Split() method splits a string where a period, question mark, exclamation mark, or a semicolon occurs.
The Split() method will return a list of all sentences from which you can extract the last one. Here is the example script:
using System; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "The car is red; It is an automatic car. with red windows! Do you like it?"; Regex myRegex = new Regex(@"[;,.?!]"); var results = myRegex.Split(textFile); Console.WriteLine(results[results.Length - 2]); } } }
In the output below, you can see the last sentence from the input text string.
Replacing the Word at the End of a String
Replacing the word at the end of a string with another word is a two-step process. You first have to find the word at the end of a string and then replacing it with another word. You have already seen how to find the word at the end of a string using the Split() function and the regular expression “\s+”. The Split() function returns the list of all words from which you can get the last word using index 1 less than the string length.
Next, to replace the last word, you can use the Replace() function. The regex expression will contain the word that you need to replace. The replace function accepts the string in which you want to replace the last word, the text of the new word, and 1 which corresponds to only a single change. Here is an example:
using System; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "Lions are one of the biggest cats in the world"; Regex myRegex = new Regex(@"\s+"); var results = myRegex.Split(textFile); string lastword = results[results.Length - 1]; Regex myRegex2 = new Regex(lastword); string result = myRegex2.Replace(textFile, "Jungle", 1); Console.WriteLine(result); } } }
Here is the output of the above script:
Other useful articles:
- How to Use RegEx for Data Extraction
- How to Find Total Tax Using a Regular Expression in C#
- How to Find a Number Using Regular Expressions in C#
- How to Find Invoice Numbers Using Regular Expressions in C#
- Find SSN Using a Regular Expression in C#
- Find Total Amount Using a Regular Expression in C#
- How to Find Website Links using Regex
- Module 1: Regular Expressions for Beginners
- Module 1: Regex Usage and Tool Demo
- Module 2: Regex Engine Basics (Part 1)
- Module 2: Regex Engine Basics (Part 2)
- Module 2: Regex Syntax in Detail (Part 1)
- Module 2: Regex Syntax in Detail (Part 2)
- Module 2: Quantifiers in Reg Ex for Beginners
- Module 2: Short Codes in Reg Ex for Beginners
- Module 2: Anchors and Boundaries in Detail
- Module 2: Grouping and Subpattern in Detail
- Module 3: Realtime Use Case of Regular Expressions - Part 1
- Module 3: Realtime Use Case of Regular Expressions - Part 2
- Module 3: Realtime Use Case of Regular Expressions - Part 3
- Module 3: Realtime Use Case of Regular Expressions - Part 4
- How to Find Quantity Field Using Regular Expression in C#
- How to Find Phone Numbers without a Specific Format
- How to Find Date Using Regular Expression in C#
- How to Find Time Using Regular Expression in C#
- How to Find a Sentence Using Regular Expressions in C#
- Find a Word Using Regular Expression in C#
- Find a Due Date using Regular Expressions in C#
- How to Find the End of a String Using Regular Expression in C
- How to Find the Start of a String Using Regular Expression in C
- How to Find a Comma using Regular Expression in C Sharp
- How to Find a Dot using Regular Expression in C
- How to Find a Semicolon using Regular Expression in C Sharp
- How to Find a Double Space using Regular Expression in C