User283571144 posted
Hi kishore309,
I'm working VS2013. I have a input string like "110(4 + 1) 3(6 + 1.5)". It should extract data like this return two rows data.
According to the problem you described, you could use the split and match methods in regular expressions to segment and match what you need to display here.
Below is an example of what I did according to your request:
First, enter string in textbox, such as "110 (4 + 1) 3 (6 + 1.5)", when you click on the button, we will first use a regular expression to determine whether the string entered in your textbox conforms to the rule you provided.
If it does, the split method of regular expression will be used to divide the input string into two parts, one is the value of Name and the other is the value of Number.
I use table to dynamically create the result result1, result2… that you need to output in the code behind.
Judging by the partitioning content, there are several results, that is, tables have several columns, and then the values they need are put into the cell through column loops.
It's important to note that the Unit content in your output should be further partitioned with Regex to get the first value in parentheses.
More details, you could refer to below codes:
ASPX:
<asp:TextBox ID="TextBox1" runat="server" Width="500px"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br /><br />
<table id="example" style="width: auto; height: auto; text-align:left" runat="server" cellspacing="0">
Code-behind:
protected void Button1_Click(object sender, EventArgs e)
{
string txt = TextBox1.Text.Trim();
Regex allrgex = new Regex(@"[0-9]+([.]{1}[0-9]+){0,1}(\()[0-9]+([.]{1}[0-9]+){0,1}(\s+).*([+-]?)(\s+).*[0-9]+([.]{1}[0-9]+){0,1}(\))");
// Determine whether the input format is correct
if (!allrgex.Match(txt).Success)
{
Response.Write("please enter right text!");
return;
}
// segment it
Regex reg = new Regex(@"(\([^\)]+\))");
Match m = reg.Match(txt);
if (m.Success)
{
string[] results = reg.Split(txt);
int column = (results.Count() - 1) / 2;//get the count of results
//Create header rows
HtmlTableRow trHead = new HtmlTableRow();
for (int i = 1; i <= column; i++)
{
HtmlTableCell headColumnCell = new HtmlTableCell();
headColumnCell.InnerHtml = "<b><y>Result" + i.ToString() + ":</u></b>";
trHead.Cells.Add(headColumnCell);
}
example.Rows.Add(trHead);
//Create data rows
HtmlTableRow tr1 = new HtmlTableRow();
int k = 1;
for (int i = 1; i <= column; i++)
{
HtmlTableCell tc1 = new HtmlTableCell();
tc1.InnerHtml = "Name = " + results[k];
//Assign values to header cells
tr1.Cells.Add(tc1);
k += 2;
}
example.Rows.Add(tr1);
HtmlTableRow tr2 = new HtmlTableRow();
int j = 0;
for (int i = 1; i <= column; i++)
{
HtmlTableCell tc1 = new HtmlTableCell();
tc1.InnerHtml = "Number = " + results[j];
//Assign values to header cells
tr2.Cells.Add(tc1);
j += 2;
}
example.Rows.Add(tr2);
HtmlTableRow tr3 = new HtmlTableRow();
int l = 1;
for (int i = 1; i <= column; i++)
{
HtmlTableCell tc1 = new HtmlTableCell();
string param = results[l].Replace("(", "").Replace(")", "");//remove ()
Regex rg = new Regex(@"^([^+]*)+");
Match o = rg.Match(param);
param = o.Groups[0].ToString();//get the number before +
tc1.InnerHtml = "Unit = " + param;
tr3.Cells.Add(tc1);
l += 2;
}
example.Rows.Add(tr3);
}
}
Result:

Best Regards,
Brando