Link Search Menu Expand Document

How to Find a Comma using Regular Expression in C Sharp

Finding special characters such as commas, periods, dashes, etc, is a common natural language processing task. Commas signify correspond to pauses in speech pronunciation. In this article, you will see how to find a comma inside text strings using regular expressions in C#. So, let’s begin without any ado.

Finding if a Text String Contains a Comma

To find if a text string contains commas, you can use the Matches() function from the C# Regex class. The Matches() function can return all instances of commas within a string. To do so, you need to pass the regex pattern “,” to the constructor of the Regex class. Next, you need to call the Matches() method using the Regex class object and pass it to your input string in which you want to find commas.

Here is an example:

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
class Program
{
static void Main(string[] args)
{
string textFile = "Is there, a comma, in this, sentence?";
Regex myRegex = new Regex(@",");
var result = myRegex.Matches(textFile);

if (result.Count > 0)
Console.WriteLine("The sentence contains " + result.Count.ToString() + " comma(s)");

Console.ReadLine();
}
}
}

The script above will search for all the instances of a comma within a string and will return the number of instances found within a string. Since the string variable textField contains 3 commas, you will see the following output:

Regular Expressions

Finding the Indexes of Commas within a String

To find indexes of commas within a string, you can again use the Matches() function. The Matches() function actually return instances of Match objects which you can iterate using a for a loop. To print the value of each matched index, you can use the Index attribute of the Match object. Here is an example.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
class Program
{
static void Main(string[] args)
{
string textFile = "Is there, a comma, in this, sentence, or not.";
Regex myRegex = new Regex(@",");
var result = myRegex.Matches(textFile);

if (result.Count > 0)
{
foreach(Match m in result)
{
Console.WriteLine(m.Index);
}
}

Console.ReadLine();
}
}
}

In the above script, the string variable textFile contains a string with four commas. In the output, you will see the index numbers for these four commas. Here is the output of the above string:

Regular Expressions C Sharp

Replacing a Comma with Other Characters

You will often need to replace commas with other characters. In C#, you can use the Regex class’ Replace() method to replace commas in a string with other characters. You need to pass the regex expression “,” to the Regex class constructor and then call the Replace() method. You have to pass the input string in which you want to replace commas, as the first parameter. The second parameter value will be the character that you want to replace. Here is an example:

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
class Program
{
static void Main(string[] args)
{
string textFile = "Is there, a comma, in this, sentence?";
Regex myRegex = new Regex(",");
var result = myRegex.Replace(textFile, "-");

Console.WriteLine(result);
Console.ReadLine();
}
}
}

The above script will replace three commas in the input string with dashes. Here is the output of the above script:

Regular Expressions C Programming

Splitting a String using a Comma as a Delimiter

Finally, you can also split a string using a comma as a delimiter.  To do so, you can use the Regex class’ Split()  function. You need to pass a comma “,” as the regex value to the Regex class constructor.

Next, you need to call the Split() method using the regex class constructor and pass the input text string to the method. The Split() method returns a list of substrings delimited using a comma character.

Here is an example of the Split() function splitting a single string into 4 substrings.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
class Program
{
static void Main(string[] args)
{
string textFile = "This string, has multiple, commas, characters.";
Regex myRegex = new Regex(",");
var result = myRegex.Split(textFile);

foreach (string str in result)
Console.WriteLine(str);

Console.ReadLine();
}
}
}

In the above script, the input string has three commas. The Split() function splits the string at the locations where it encounters a comma. As a result, the following 4 substrings are printed on the console.

Regular Expressions C

Other useful articles:


Back to top

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