Home > Blockchain >  Get the list of items matching a specific pattern of string
Get the list of items matching a specific pattern of string

Time:11-16

I have a C# model LedgerEntity.

public class LedgerEntity
{
     public string AccountNumber{get;set;}
     public string Accountlevel{get;set;}
     public string AccountName{get;set;}
}

Also I have a list of items of that model class like below

List<LedgerEntity> items=new List<LedgerEntity>();

items=[
{"AccountNumber":"07-01-GRP_10095-81120000","AccountLevel":"Group","AccountName":"JPM"},
{"AccountNumber":"G3-80-GRP_10895-8112SLC0","AccountLevel":"Group","AccountName":"CIT"},
{"AccountNumber":"1C-MULTI-2851170-MULTI_8113xxxx","AccountLevel":"Group","AccountName":"BON"},
{"AccountNumber":"07-MULTI-MULTI_NONCORE-MULTI","AccountLevel":"Group","AccountName":"CUK"}}
]

My requirement is some thing like this. I need to get all the records which contains

"GRP_*8112(* can have any value like 10095- or 10895- etc...)" in `AccountNumber. Then remove "GRP_" from the record and add "70" in front of the 3rd hyphen.

Example: 07-01-GRP_10095-81120000 --> 07-01-1009570-81120000

also update the Accountlevel of that matched records from Group to Individual.

My expected output is something like this.

matchedItems=[
{"AccountNumber":"07-01-1009570-81120000","AccountLevel":"Individual","AccountName":"JPM"},
{"AccountNumber":"G3-80-1089570-8112SLC0","AccountLevel":"Individual","AccountName":"CIT"},
{"AccountNumber":"1C-MULTI-2851170-MULTI_8113xxxx","AccountLevel":"Group","AccountName":"BON"},
{"AccountNumber":"07-MULTI-MULTI_NONCORE-MULTI","AccountLevel":"Group","AccountName":"CUK"}}
]

Please help me on this.

CodePudding user response:

your problem can be solved with Regex with an expression like this for example:

(10(8|0)95)

You can use following code:

items.Where(item => Regex.IsMatch(item.AccountNumber, @"10(8|0)95)"))

CodePudding user response:

try this

    foreach (var item in items)
    {
        var grpIndex = item.AccountNumber.IndexOf("GRP_");
        if (grpIndex >= 0)
        {
            var numberIndex = item.AccountNumber.Substring(grpIndex   4).IndexOf("8112");
            if (numberIndex >= 0)
            {
                item.AccountNumber = item.AccountNumber.Replace("GRP_", "");
                item.Accountlevel = "Individual";
                var i = 0;
                var counter = 0;
                foreach (var ch in item.AccountNumber)
                {
                    if (ch == '-') counter  ;
                    if (counter == 3) break;
                    i  ;
                }
                if (counter == 3) item.AccountNumber = item.AccountNumber.Insert(i, "70");
            }
        }
    }
  •  Tags:  
  • c#
  • Related