Link Search Menu Expand Document

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:

RegEx Sentence

 

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

 

RegEx How to Find Sentence

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

}
}
}

 

RegEx Find Sentence

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.

 

RegEx Finding Sentence

Other useful articles:


Back to top

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