Read and extract value from string using RegularExpression in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

Hi

I am trying to read a long string and i want to extract from that string my required information

I trying regular expression for example 

PO No: 5293274630 Date: 2020.03.16 

To extract 5293274630 this i wrote 

var PONO = Regex.Match(result, @"(?<=PO No :)(\s\d{10}\s)(?=Date :)");

This is working fine but i want to make a pattern for this for example 

Material Description Commercial-Code 1 MI-SCBSMT24SM 10 45.62 1 PC 456.20 2020.03.18 MI-SCBSMT24SM,, MI-SCBSMT24SM

Here i need to write regex to extract MI-SCBSMT24SM 

String fileInformation = "Purchase Order PO No : 5293274630 Date : 2020.03.16 Supplier Name & Address Buyer Name & Address L10IU1 : ECONOCOM INTERNATIONAL ITALIA S.P.A Samsung Electronics Italia S.p.A Via Marcello Nizzoli, 8 Via Mike Bongiorno, 9 MILANO 20124 Sales Person : 2061971 ESPRI Purchse Group : QSW Payment Terms : S160 Purchaser : AAAAA Currency : EUR Plant : S425 Destination : CIF / MILANO [SEI]Plant TO SITE : Use Code : I Material Qty Price Per Unit Amount Delivery Material Description Commercial-Code 1 MI-SCBSMT24SM 10 45.62 1 PC 456.20 2020.03.18 MI-SCBSMT24SM,, MI-SCBSMT24SM 2 MI-SCBSML24SM 10 32.28 1 PC 322.80 2020.03.18 MI-SCBSML24SM,, MI-SCBSML24SM Amount : 779.00 Remark 1 / 1";
string line;    //- Holds the entire line

//- Cycle thru the text file 1 line at a time pulling
//- substrings into the variables initialized above
while ((line = sr.ReadLine()) != null)
{
    //- Pulling substrings.  If there is a problem
    //- with the start index and/or the length values
    //- an exception is thrown
    try
    {
        if (line.Trim().Contains("PO No"))
        {
            //Regex regex = new Regex(@"\d{10}");
            Match matchOrder = Regex.Match(line.Trim(), @"\d{10}", RegexOptions.IgnoreCase);

            if (matchOrder.Success)
            {
                samsungOrder.OrderNumber = matchOrder.Value;
            }

            Match matchOrderDate = Regex.Match(line.Trim(), @"\d{4}[.]\d{2}[.]\d{2}", RegexOptions.IgnoreCase);

            if (matchOrderDate.Success)
            {
                samsungOrder.OrderDate = matchOrderDate.Value.Replace(".", "-");
            }

            //samsungOrder.ExternelReference = Path.GetFileNameWithoutExtension(fileName);
        }

 

You are viewing reply posted by: dharmendr one month ago.
Posted one month ago

Use this regularexpression.

([A-Za-z]{2}[-]{1}[A-Za-z]{6}[0-9]{2}[A-Za-z])\w+