Link Search Menu Expand Document

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

Regex C Sharp End of String

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

Regex C Sharp Find End of String

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.

Regular Expressions End of 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:

Regex C Sharp How to Find End of String

Other useful articles:


Back to top

© , Regexsonline.com — All Rights Reserved - Terms of Use - Privacy Policy