Solved

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

Posted on 2010-09-02
8
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 27

Expert Comment

by:nmarun
ID: 33594076
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
ID: 33594314
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
ID: 33594398
but you can not know 5. character. it can be 4. or 3. or 6. character. we consantrate on "-" character...
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 6

Accepted Solution

by:
nipunu earned 500 total points
ID: 33594524
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
 
LVL 27

Expert Comment

by:nmarun
ID: 33595119
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
ID: 33595373
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
ID: 33597679

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
ID: 33838929
Any update on this?

Arun
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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