How to Find a Sentence Using Regular Expressions in C#
In this article, you will see how you can find a sentence within a text using C# regular expression. You will study how to search for a complete sentence within a string, how to find a sentence at the beginning of a string, how to find a sentence at the end of a string, and how to find sentences that contain a particular word. So, let’s begin without any ado.
Searching a Sentence Anywhere in a String
To search for a complete sentence at any location inside a text string in C#, you can use the Matches() function from the C# regex library. First, you need to create an object of the Regex class and pass it the sentence that you want to search. Next, you need to call the Matches() function using the Regex class object. The Matches function accepts the text string to be searched as a parameter.
The following C# script searches for the sentence “The car is red” in the string variable textFile. If the sentence is found an appropriate message is displayed to the user on the console window.
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? The car is red"; Regex myRegex = new Regex(@"The car is red"); var results = myRegex.Matches(textFile); if (results.Count > 0) Console.WriteLine("The sentence is found " + results.Count.ToString() + " times."); else Console.WriteLine("Sentence not found."); } } }
In the input string textFile, the sentence “The car is red” is found twice, therefore you can see the following output:
Finding the First Sentence
To find the first sentence, you can use the Match() method from the Regex class. The regex operation “^.*?.?!;” can be used to search for the first sentence in the text. The expression basically returns all the text before a period, question mark, exclamation mark, or a semicolon, that follows an empty white space. Here is an example script that can be used to return the first sentence in a text.
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!"; Regex myRegex = new Regex(@"^.*?[.?!;](?=\s+)"); var results = myRegex.Match(textFile); var text = results.Groups[0].Value; Console.WriteLine(text); } } }
Finding the Last Sentence
Finding the last sentence in a string is also super easy. One way to do it is to break a long text into multiple sentences at locations where a period, question mark, exclamation mark, or semicolon occurs. The Split() method from Regex can be used to split a text. The regex operation to be used is: [;,.?!]. This regex expression will split a string wherever a semi-colon, a comma, a period, a question mark, or an exclamation mark occurs. The Split() method will return a list of all sentence split at the locations specified by the regex expression. From the list of these sentences, you can then extract the last sentence.
Here is the example script which prints the last sentence on the console output, from a list of sentences in a 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 = "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]); } } }
Finding Sentences with a Specific Word
To find all the sentences with specific words using C# regex, you can use the Matches() function. For example, in the following script the regex [^.!?;]*(red)[^.!?;] extracts all the sentences that contain the word “red”. The regex expression here searches for any string that starts with or ends with a period, an exclamation mark, a question mark, or a semicolon and contains the word “red”.
Look at the following example for reference.
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!"; Regex myRegex = new Regex("[^.!?;]*(red)[^.!?;]*"); var results = myRegex.Matches(textFile); foreach (var sent in results) { Console.WriteLine(sent.ToString().Trim()); } } } }
In the above script, the input string contains two sentences with the word “red”. In the output, you will see these two sentences.
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