Hi @Padmanabhan, Venkatesh ,Welcome to Q&A. You could try the following code to get what you wanted.
Note that this code is only applicable when your strings are in the correct format and are all in groups of 5. The judgment of the error situation is not added.
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class region_case
{
public string Region { get; set; }
public string Frequency { get; set; }
public string Name { get; set; }
}
List<region_case> list = new List<region_case>();
private void button1_Click(object sender, EventArgs e)
{
string str = "where os_name in ('Region','Test-Daily','WWP','WWP3D','WWPF3D','Region','Test-BackDaily','P01D','WPI1D','WPF1D','Region','Test-CountryDaily','WIP02D','WPI2D','WPF2D','Region','Test-CountoDaily','DAIP04D','DAIPI4D','PF4D')";
string pattern = @"'(.*?)'"; //Regular expression pattern, match content inside single quotes
MatchCollection matches = Regex.Matches(str, pattern); // Perform regular expression matching
for (int i = 0; i < matches.Count; i++)
{
string value = matches[i].Groups[1].Value;
if (value == "Region")
{
region_case region = new region_case();
region.Region = value;
region.Frequency = matches[i + 1].Groups[1].Value;
for (int j = 2; j < 5; j++)
{
region.Name = matches[i + j].Groups[1].Value;
list.Add(region);
}
}
}
dataGridView1.DataSource = list;
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.