我现在有多行字符串,现在我想打印包含特定子字符串的那些行

时间:2018-09-21 10:55:01

标签: javascript arrays node.js string npm

       protected void btnUpload_Click(object sender, EventArgs e)
{
    try
    {
        int id;
        string contactPerson;
        string designation;
        string company;
        string contact;
        string emailaddress;
        string city;
        string region;
        string industry;
        string division;
        string mobile;
        string address;

        string path = Path.GetFileName(FileUpload1.FileName);
        path = path.Replace(" ", "");

        FileUpload1.SaveAs(Server.MapPath("~/uploadExcel/") + FileUpload1.FileName);
        String ExcelPath = Server.MapPath("~/uploadExcel/") + FileUpload1.FileName;
        OleDbConnection mycon = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + ExcelPath + "; Extended Properties=Excel 8.0; Persist Security Info = False");

        mycon.Open();

       DeleteRecords();

        OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", mycon);
        OleDbDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            if (dr[0].ToString() != "")
            {
                // Response.Write("<br/>"+dr[0].ToString());
                id = Convert.ToInt32(dr[0].ToString());
                contactPerson = dr[1].ToString();
                designation = dr[2].ToString();
                company = dr[3].ToString();
                emailaddress = dr[4].ToString();
                contact = dr[5].ToString();
                mobile = dr[6].ToString();
                address = dr[7].ToString();
                city = dr[8].ToString();
                region = dr[9].ToString();
                industry = dr[10].ToString();
                division = dr[11].ToString();

                InsertTemp(id, contactPerson, designation, company, emailaddress, contact,
                 mobile, address, city, region, industry, division);

                //InsertOrignal(id, contactPerson, designation, company, emailaddress, contact,
                // mobile, address, city, region, industry, division);

            }

            else
            {
                break;
            }


            String myconn = "Data Source=Ali-PC;Initial Catalog=MushkhoApp;Integrated Security=True";
            SqlConnection conn = new SqlConnection(myconn);
            conn.Open();
            DataTable dt_temp = new DataTable();
            DataTable dt_orignal = new DataTable();
            SqlDataAdapter da_temp = new SqlDataAdapter("select * from Tbl_ExcelData order by id asc", conn);
            SqlDataAdapter da_orignal = new SqlDataAdapter("select * from Tbl_ExcelUploadData order by id asc", conn);

            da_temp.Fill(dt_temp);
            da_orignal.Fill(dt_orignal);


            if (dt_orignal.Rows.Count > 0)
            {

                for (int i = 0; i < dt_temp.Rows.Count; i++)
                {
                    for (int j = 0; j < dt_orignal.Rows.Count; j++)
                    {
                        if (dt_temp.Rows[i]["email"].ToString() == dt_orignal.Rows[j]["email"].ToString())
                        {
                            //Update Record if required
                        }
                        else
                        {
                            //insert record into orignal table


                            InsertOrignal(id, contactPerson, designation, company, emailaddress, contact, mobile, address, city, region, industry, division);


                        }
                    }

                }
            }

            else
            {
                InsertOrignal(id, contactPerson, designation, company, emailaddress, contact, mobile, address, city, region, industry, division);

            }

        }

       lblmessage.Text = "Data Has Been Updated Successfully";

        mycon.Close();
        File.Delete(ExcelPath);

    }
    catch (Exception ex)
    {

        Console.WriteLine(ex.Message);
    }



}



private void InsertTemp(int id, String contactPerson, String designation, String company, String emailaddress, 
                             String contact, String mobile, String address,String city,String region,String industry,
                             String division)
{


    //String mycon = "Data Source=Ali-PC;Initial Catalog=MushkhoApp;Integrated Security=True";
    SqlConnection con = new SqlConnection(mycon);
    con.Open();




            string query = "insert into Tbl_ExcelData (id,contactperson,designation,company,email,contact,mobile,address,city,region,industry,division) values('" + id + "','" + contactPerson + "', '" + designation + "','" + company + "','" + emailaddress + "','" + contact + "','" + mobile + "','" + address + "','" + city + "','" + region + "','" + industry + "','" + division + "')";

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = query;
            cmd.Connection = con;
            cmd.ExecuteNonQuery();

        }


private void InsertOrignal(int id, String contactPerson, String designation, String company, String emailaddress,
                            String contact, String mobile, String address, String city, String region, String industry,
                            String division)
{


    //String mycon = "Data Source=Ali-PC;Initial Catalog=MushkhoApp;Integrated Security=True";
    SqlConnection con = new SqlConnection(mycon);
    con.Open();




    string query = "insert into Tbl_ExcelUploadData (id,contactperson,designation,company,email,contact,mobile,address,city,region,industry,division) values('" + id + "','" + contactPerson + "', '" + designation + "','" + company + "','" + emailaddress + "','" + contact + "','" + mobile + "','" + address + "','" + city + "','" + region + "','" + industry + "','" + division + "')";

    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = query;
    cmd.Connection = con;
    cmd.ExecuteNonQuery();

}


private void DeleteRecords()
{

    SqlConnection con = new SqlConnection(mycon);
    con.Open();
    string query = "Delete from Tbl_ExcelData";
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = query;
    cmd.Connection = con;
    cmd.ExecuteNonQuery();

}


 }

这里是多行:现在找到包含子字符串Object的那些行。 <匿名>

Here i want to print line which contains following strings: 
Object.< anonymous > 

输出应为: 在对象上。<匿名>(/home/abc/Desktop/AJ/CustomLogger/conn.js:10:5)

1 个答案:

答案 0 :(得分:2)

您可以拆分字符串并首先获取所有行,然后检查Object.< anonymous >(有无regex) 过滤掉它们。

let str = `Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'yourusername'@'localhost' (using password: YES)
at Socket.< anonymous > (/home/abc/Desktop/AJ/CustomLogger/node_modules/mysql/lib/Connection.js:91:28)
at Object.< anonymous > (/home/abc/Desktop/AJ/CustomLogger/conn.js:10:5)
at Module._compile (internal/modules/cjs/loader.js:686:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)`,

res = str.split("\n").filter(line=>line.indexOf("Object.< anonymous >")>-1);

console.log(res);

或者,您也可以将regex与带有多行标记(m)匹配

let str = `Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'yourusername'@'localhost' (using password: YES)
at Socket.< anonymous > (/home/abc/Desktop/AJ/CustomLogger/node_modules/mysql/lib/Connection.js:91:28)
at Object.< anonymous > (/home/abc/Desktop/AJ/CustomLogger/conn.js:10:5)
at Module._compile (internal/modules/cjs/loader.js:686:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)`,

res = str.match(/^(.*Object\.< anonymous >.*)$/mg);

console.log(res);