locked
Regex for retriving values RRS feed

  • Question

  • User846736780 posted

    I'm new to Regex. And I need Regex for following string

    1) Property NodeDisplacements.Dx falls outside the acceptable tolerance (Node number 49, load case 1).
    Expected: 0.2524431049823761d +/- 0.01d or 0.2524431049823761d +/- 2.0d Percent
    But was: 0.24117276072502136d

    2) Property MemberEndForces.Fx falls outside the acceptable tolerance (Beam number 1, load case 1, beam end EndB).
    Expected: -1.166001558303833d +/- 0.001d or -1.166001558303833d +/- 2.0d Percent
    But was: -1.1420921087265015d

    3)Property MemberSectionForces.Mz falls outside the acceptable tolerance (Beam number 33, load case 1).
    Expected: < 60.961132049560547d, 50.785896301269531d, 40.610660552978516d, 30.4354248046875d, 20.260189056396484d, 10.084957122802734d, -0.090282440185546875d, -10.265514373779297d, -20.440753936767578d, -30.615993499755859d, -40.791217803955078d, -50.966464996337891d, -61.141693115234375d > +/- 0.01d or < 60.961132049560547d, 50.785896301269531d, 40.610660552978516d, 30.4354248046875d, 20.260189056396484d, 10.084957122802734d, -0.090282440185546875d, -10.265514373779297d, -20.440753936767578d, -30.615993499755859d, -40.791217803955078d, -50.966464996337891d, -61.141693115234375d > +/- 3.0d Percent
    But was: < 61.908939361572266d, 51.576515197753906d, 41.244094848632812d, 30.911674499511719d, 20.579250335693359d, 10.246833801269531d, -0.085590362548828125d, -10.418010711669922d, -20.750438690185547d, -31.082859039306641d, -41.415271759033203d, -51.747707366943359d, -62.080116271972656d >

    String variable contain any of above value.So I have read node number,load case,expected and but was value in 1st case.  and in 2nd case beam number (as node number is not there) and everything mentioned in case 1. and in 3rd case have to read all expected and but was value.

    Expected result:

    1) Node number 49  load case 1  Expected: 0.2524431049823761 Tolerance:0.01 Tolerance percent :2.0  But was: 0.24117276072502136

    2) Beam number 1 load case 1 Expected: -1.166001558303833 Tolerance:0.001 Tolerance percent :2.0 But was: -1.1420921087265015

    3) Beam number 33 load case 1 Expected: < 60.961132049560547d, 50.785896301269531d, 40.610660552978516d, 30.4354248046875d, 20.260189056396484d, 10.084957122802734d, -0.090282440185546875d, -10.265514373779297d, -20.440753936767578d, -30.615993499755859d, -40.791217803955078d, -50.966464996337891d, -61.141693115234375d > Tolerance:0.01 Tolerance percent : 3.0

    so I need a regex which will handle all above 3 cases.

    Thursday, May 16, 2019 4:57 AM

Answers

  • User665608656 posted

    Hi Pavan Patil,

    According to the question you described, you could use the split and match methods in regular expressions to segment and match what you need to display here. 

    My idea is:

    Put the three strings in the<p> which id='beforeContent' when the page is loaded, and add < br/>  after each string to separate the three strings.

    Click the button to get the contents of the <p> which id='beforeContent'.

    Then, string is separated from each other by regex split method according to <br/>, and three strings are converted into the required format by the same regex method. 

    Finally, the converted string is assigned to the <p> which id='afterContent'.

    For more details, refer to the following code:

    <form id="form1" runat="server">
            <div>
                before:
                <br />
                <div style="width: 100%; height: auto;">
                    <p id="beforeContent" runat="server"></p>
                </div>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Regex" OnClick="Button1_Click1" />
                <br />
                <br />
                after:
                <br />
                <br />
                <div style="width: 100%; height: auto;">
                    <p id="afterContent" runat="server"></p>
                </div>
            </div>
        </form>

    code behind:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    string txt1 = @"1) Property NodeDisplacements.Dx falls outside the acceptable tolerance (Node number 49, load case 1).
    Expected: 0.2524431049823761d +/- 0.01d or 0.2524431049823761d +/- 2.0d Percent
    But was: 0.24117276072502136d";
                    string txt2 = @"2) Property MemberEndForces.Fx falls outside the acceptable tolerance (Beam number 1, load case 1, beam end EndB).
    Expected: -1.166001558303833d +/- 0.001d or -1.166001558303833d +/- 2.0d Percent
    But was: -1.1420921087265015d";
                    string txt3 = @"3)Property MemberSectionForces.Mz falls outside the acceptable tolerance (Beam number 33, load case 1).
    Expected: < 60.961132049560547d, 50.785896301269531d, 40.610660552978516d, 30.4354248046875d, 20.260189056396484d, 10.084957122802734d, -0.090282440185546875d, -10.265514373779297d, -20.440753936767578d, -30.615993499755859d, -40.791217803955078d, -50.966464996337891d, -61.141693115234375d > +/- 0.01d or < 60.961132049560547d, 50.785896301269531d, 40.610660552978516d, 30.4354248046875d, 20.260189056396484d, 10.084957122802734d, -0.090282440185546875d, -10.265514373779297d, -20.440753936767578d, -30.615993499755859d, -40.791217803955078d, -50.966464996337891d, -61.141693115234375d > +/- 3.0d Percent
    But was: < 61.908939361572266d, 51.576515197753906d, 41.244094848632812d, 30.911674499511719d, 20.579250335693359d, 10.246833801269531d, -0.085590362548828125d, -10.418010711669922d, -20.750438690185547d, -31.082859039306641d, -41.415271759033203d, -51.747707366943359d, -62.080116271972656d >";
                    beforeContent.InnerHtml = txt1 + "<br/><br/>" + txt2 + "<br/><br/>" + txt3;
                }
            }
    
            private void RegexChange(string txt)
            {
                Regex reg = new Regex(@"<br\/>|\|");
                string[] resultLists = reg.Split(txt);
                string t1 = "";
                for (int i = 0; i < resultLists.Count(); i++)
                {
                    if (resultLists[i] != "")
                    {
                        string txt1 = resultLists[i].Trim();
                        reg = new Regex(@"(\([^\)]+\))");
                        Match m = reg.Match(txt1);
                        if (m.Success)
                        {
                            string[] results = reg.Split(txt1);
                            string[] split0 = Regex.Split(results[1].Replace("(", "").Replace(")", "").Trim(), @",|\|");
                            t1 += results[0].Substring(0, 2).Trim() + " " + split0[0] + " " + split0[1];
                            // t1 = results[0].Substring(0, 2).Trim() + " " + results[1].Replace("(", "").Replace(")", "").Replace(",", " ").Trim();//remove ()
                            string[] split1 = Regex.Split(results[2].Trim(), @"or|\|");
                            string[] split2 = Regex.Split(Regex.Split(split1[0].Trim(), @"\r\n|\|")[1].Trim(), @"\+\/\-|\|");
                            string[] split3 = Regex.Split(Regex.Split(split1[1].Trim(), @"\r\n|\|")[0].Trim(), @"\+\/\-|\|");
    
                            t1 += " " + (split2[0].Trim().Substring(split2[0].Trim().Length - 1, 1) == "d" ? (split2[0].Trim().Substring(0, split2[0].Trim().Length - 1)) : (split2[0].Trim())) + " Tolerance:" + split2[1].Trim().Substring(0, split2[1].Trim().Length - 1);
                            t1 += " Tolerance percent :" + Regex.Split(split3[1].Trim(), @"d|\|")[0].Trim();
                            t1 += "  " + (Regex.Split(split1[1].Trim(), @"\r\n|\|")[1].Trim().Substring(Regex.Split(split1[1].Trim(), @"\r\n|\|")[1].Trim().Length-1, 1) == "d" ? (Regex.Split(split1[1].Trim(), @"\r\n|\|")[1].Trim().Substring(0, Regex.Split(split1[1].Trim(), @"\r\n|\|")[1].Trim().Length - 1)) : Regex.Split(split1[1].Trim(), @"\r\n|\|")[1].Trim());
                            t1 += "<br/><br/>";
                        }
                    }
                }
                afterContent.InnerHtml = t1;
            }
    
            protected void Button1_Click1(object sender, EventArgs e)
            {
                string txt1 = beforeContent.InnerHtml;
                RegexChange(txt1);
            }

    The result of my work demo:    

       


    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 17, 2019 8:27 AM