Link Search Menu Expand Document

How to Find the Start of a String Using Regular Expression in C Sharp

This article shows how you can search for a word or sentence at the beginning of a text string using C# regular expressions. You will also see which words or sentences occur at the beginning of a string, and how to replace the first word of a string. So, let’s start without any further ado.

Searching the Start of a String for Existence of Text

You can check if a certain text exists at the beginning of a string using the Match() function from the Regex class. You have to pass the expression “^word_to_search” to the Regex class instructor. The expression “^word_to_search” tells the compiler to look for the specified word at the beginning of the sentence. If the word is found, the success attribute of the object returned by the Match() method will become true.

The following script searches for the word “Lions” at the start of a text 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(@"^Lions");
var results = myRegex.Match(textFile);

if (results.Success)
Console.WriteLine("Word found at the start");
else
Console.WriteLine("Word not found at the start");

}
}
}

Regex C Sharp Find Start of String

Extracting a Word from the Start of String

You can also extract a word from the start of a string using C# Regex. You can use the Split() function with the expression “\s+” which splits the string using a white space delimiter and returns a list of words. You can then extract the first item from the list which contains the first word of the string. 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);

Console.WriteLine("The word at the start of the string is: " + results[0]);

}
}
}

C Sharp Find Start of String

Extracting a Sentence from the Start of String

To find the sentence at the beginning of a string, you can use the Match() method from the Regex class. The regex operation used to search for a sentence at the beginning of a string is “^.*?.?!;”. The expression returns string text before a period, question mark, exclamation mark, or a semicolon, that follows an empty white space. You can add other characters too that mark the end of a sentence if you want. 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 = "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);
}
}
}

The output shows the text “The car is red” which is the first sentence in the input string textFile.

Regex C Find Start of String

Replacing the Word at the Start of a String

To replace a word at the beginning of a string, you first have to follow the first word and then replace it. To find the first word of a string, you can use the Split() function and pass it the regex expression “\s+”. This regex expression will split the string at empty spaces. The Split() function will a return list of all words, from which you can get the first word by passing 0 as the list index.

Once you have retrieved the first word from a list, you can replace it with a string using the Replace() function from the Regex. You need to pass the string and the new word that you want to replace the old word with. For example, in the following script, the first word of the string textFile is replaced by the word “Whales”.

The Replace() function actually replaces all the occurrences of the passed word, however, if you want to replace only the first word, you need to pass 1 as the value for the third parameter of the Replace() function.

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 firstword = results[0];

Regex myRegex2 = new Regex(firstword);
string result = myRegex2.Replace(textFile, "Whales", 1);
Console.WriteLine(result);
}
}
}

How to Find Start of String

Other useful articles:


Back to top

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