Split regular expression

Pranat
0
Lamp

 using System.Text;

using System.Text.RegularExpressions;

public class Example

{

 public static void Main()

 {

 string input = "Amar,Akbar,Antony are Friends!"; //Input String

 // Split Whenever the , and space is encounterd in the input string

 string pattern = "( |,)"; //RegEx Patter

 Regex regex = new Regex(pattern);

 StringBuilder str = new StringBuilder();

 //The Split() method returns An array of strings so store it in the array of String Type

 string[] substr = regex.Split(input);

 int count = 1; //In order to prfix the splitted words with counter from 1 to ....

 foreach (string match in substr)

 {

 //Eliminate the , and space in the splitted array i.e, substr otherwise we get , and space in output

 if (match == "," || match == " ")

 {

 continue;

 }

 else

 {

 //Simply format the output by prefixing the Pattern

 str.AppendFormat("{0}: {1}", count++, match);

 }

 }

 Console.WriteLine(str);

 }

}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !