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