Solved

How can i select some id with xx-xxx-xx-x format?

Posted on 2010-09-02
8
308 Views
Last Modified: 2013-11-11
i need to change below data

A320-ZL-522-01-1(2)
A320-ZL-531-01-1(1)
A320-ZL-531-01-1(2)
A320-ZL-531-01-1(1)
A320-ZL-531-01-1(2)
TO

ZL-522-01-1
ZL-531-01-1
ZL-531-01-1
ZL-531-01-1
ZL-531-01-1
Can i do it with linq using Regex?

    public class MatchCTallyToCross
    {
        public List<string> Get(string CustomerNumber)
        {
            IEngManager engManager = new EngManager();
            EngCustomerMyCross engCustomerMyCross = new EngCustomerMyCross();
            engManager.Load(engCustomerMyCross);
            return engCustomerMyCross.myEngCustomerCross.
                Where(c => c.ENG_CUSTOMERMYCROSS_CUSTOMER_NUMBER == CustomerNumber).
                Select(c => c.ENG_CUSTOMERMYCROSS_MYTECHNIC_TASK_NO).ToList();
        }
    }

Open in new window

c => c.ENG_CUSTOMERMYCROSS_MYTECHNIC_TASK_NO result is equal to "A320-ZL-531-01-1(2)" But i need "ZL-531-01-1"
0
Comment
Question by:programmerist
8 Comments
 
LVL 27

Expert Comment

by:nmarun
Comment Utility
Here's a snippet that worked for me. I did not have to use Regex.

Arun

private static void GetFormattedString()

{

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

    items.Add("A320-ZL-522-01-1(2)");

    items.Add("A320-ZL-531-01-1(1)");

    items.Add("A320-ZL-531-01-1(2)");

    items.Add("A320-ZL-531-01-1(1)");

    items.Add("A320-ZL-531-01-1(2)");



    List<string> formattedStrings = (from item in items

                                        select item.Substring(5, 11)).ToList();



    for (int i = 0; i < formattedStrings.Count; i++)

    {

        Console.WriteLine(formattedStrings[i]);

    }

}

Open in new window

0
 
LVL 6

Expert Comment

by:nipunu
Comment Utility
Use Substring
string string.substring(int startIndex,int length)


private static string getFormattedString(string originalstring)
{

string formattedString = originalstring.SubString(5,11);
return formattedString;
}

0
 

Author Comment

by:programmerist
Comment Utility
but you can not know 5. character. it can be 4. or 3. or 6. character. we consantrate on "-" character...
0
 
LVL 6

Accepted Solution

by:
nipunu earned 500 total points
Comment Utility
Then u have to use split method as well

private static string getFormattedString(string originalstring)
{

//remove the (x) part from the string
string newString = originalstring.Substring (0, sList.Length-3);

//now split it
string[] words = newString.Split('-');

string finalString = words [1] + "-"+ words [2] + "-"+ words 3] + "-"+ words [4];
return formattedString;
}
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 27

Expert Comment

by:nmarun
Comment Utility
And here's how you can do it with regex.

Arun

private static void GetFormattedString()

{

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

    items.Add("A320-ZL-522-01-1(2)");

    items.Add("A320-ZL-531-01-1(1)");

    items.Add("A320-ZL-531-01-1(2)");

    items.Add("A320-ZL-531-01-1(1)");

    items.Add("A320-ZL-531-01-1(2)");



    Regex regex = new Regex(@"[A-Za-z]{2}-\d{3}-\d{2}-\d");



    List<string> formattedStrings = (from item in items

                                        select regex.Match(item).Value).ToList();



    for (int i = 0; i < formattedStrings.Count; i++)

    {

        Console.WriteLine(formattedStrings[i]);

    }

}

Open in new window

0
 
LVL 27

Expert Comment

by:nmarun
Comment Utility
Below is the improvised version of the previous code. The last one would give you an empty string if the match failed. I've avoided this by adding a where condition to the LINQ statement.

Arun

private static void GetFormattedString()

{

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

    items.Add("A320-ZL-522-01-1(2)");

    items.Add("A320-ZL-531-01-1(1)");

    items.Add("A320-ZL-531-01-1(2)");

    items.Add("A320-ZL-531-01-1(1)");

    items.Add("A320-ZL-531-01-1(2)");



    Regex regex = new Regex(@"[A-Za-z]{2}-\d{3}-\d{2}-\d");



    List<string> formattedStrings = (from item in items

                                        let m = regex.Match(item)

                                        where m.Success

                                        select m.Value).ToList();



    for (int i = 0; i < formattedStrings.Count; i++)

    {

        Console.WriteLine(formattedStrings[i]);

    }

}

Open in new window

0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility

public class MatchCTallyToCross

        {

            public List<string> Get(string CustomerNumber)

            {

                IEngManager engManager = new EngManager();

                EngCustomerMyCross engCustomerMyCross = new EngCustomerMyCross();

                engManager.Load(engCustomerMyCross);

                return engCustomerMyCross.myEngCustomerCross.

                    Where(c => c.ENG_CUSTOMERMYCROSS_CUSTOMER_NUMBER == CustomerNumber).

                    Select(c => c.ENG_CUSTOMERMYCROSS_MYTECHNIC_TASK_NO.SubString(c.ENG_CUSTOMERMYCROSS_MYTECHNIC_TASK_NO.IndexOf("-") +1)).ToList();

            }

        }

Open in new window

0
 
LVL 27

Expert Comment

by:nmarun
Comment Utility
Any update on this?

Arun
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now