Solved

Create key and pair

Posted on 2011-09-28
6
189 Views
Last Modified: 2012-05-12
Hello, I have a string list.
GO-1212a	GO-1212b	GO-1213a	GO-1213b	GO-1214a	GO-1214b	GO-1215a	GO-1215b	GO-1216a	GO-1216b	GO-1217a	GO-1217b	GO-1218a	GO-1218b	GO-1219a	GO-1219b	GO-1220a	GO-1220b	GO-1221a	GO-1221b	GO-1222a	GO-1222b	GO-1223a	GO-1223b	GO-1224a	GO-1224b	GO-1225a	GO-1225b	GO-1226a	GO-1226b	GO-1227a	GO-1227b	NA 7029a	NA 7029b	No DNAa	No DNAb

Open in new window


I want to use regular expression to create the pairs
GO-1212a	GO-1212b
GO-1213a	GO-1213b
GO-1214a	GO-1214b
GO-1215a	GO-1215b
GO-1216a	GO-1216b
GO-1217a	GO-1217b
GO-1218a	GO-1218b
GO-1219a	GO-1219b
GO-1220a	GO-1220b
GO-1221a	GO-1221b
GO-1222a	GO-1222b
GO-1223a	GO-1223b
GO-1224a	GO-1224b
GO-1225a	GO-1225b
GO-1226a	GO-1226b
GO-1227a	GO-1227b
NA 7029a	NA 7029b
No DNAa	No DNAb

Open in new window

I used to use a per-processing data such as remote the prefix, but I am not satisfied it.Thanks
0
Comment
Question by:zhshqzyc
[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
  • 3
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36717748
Like this?

string source = "GO-1212a	GO-1212b	GO-1213a	GO-1213b	GO-1214a	GO-1214b	GO-1215a	GO-1215b	GO-1216a	GO-1216b	GO-1217a	GO-1217b	GO-1218a	GO-1218b	GO-1219a	GO-1219b	GO-1220a	GO-1220b	GO-1221a	GO-1221b	GO-1222a	GO-1222b	GO-1223a	GO-1223b	GO-1224a	GO-1224b	GO-1225a	GO-1225b	GO-1226a	GO-1226b	GO-1227a	GO-1227b	NA 7029a	NA 7029b	No DNAa	No DNAb";
string result = System.Text.RegularExpressions.Regex.Replace(source, @"(\w{2}[- ]\w+\s+\w{2}[- ]\w+)\s+", "$1\n");

Open in new window

0
 
LVL 28

Expert Comment

by:strickdd
ID: 36717749
This may not be exact because of fringe cases, but should be close for you:

[A-Za-z0-9\-]*a\t{1}[A-Za-z0-9\-]*b\t
0
 

Author Comment

by:zhshqzyc
ID: 36718597
I am sorry. It is a list of strings. I want to create a key for each pair then get each index of string in the array. How to modify the code below?
List<string> list1 = new List<string>();
list1.Add("GO-1212a");
list1.Add("GO-1212b");
....
Dictionary<string, List<int>> categorized = new Dictionary<string, List<int>>();
string[] header1 = list1.ToArray();
                for (int i = 0; i < header1.Length; i++)
                {
                    string key = header1[i].Substring(); //here need some work
                    System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(key, "^\\w{2}[- ]\\w+\\s+\\w{2}[- ]\\w+)\\s+"); //here also need work
                    if (m.Success)
                    {
                        if (!categorized.ContainsKey(m.Value))
                        {
                            categorized.Add(m.Value, new List<int>());
                        }

                        categorized[m.Value].Add(i);
                    }

                }

Open in new window

0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36719796
I don't understand where the regular expression part is supposed to come into play. Can you elaborate?
0
 

Author Comment

by:zhshqzyc
ID: 36719847
It is a long story.
Actually I want to create a dictionary, the results like

key:
GO-1012
Value:{0,1}

key:
GO-1013
Value: {2,3}

...
key:
NA 7029
Value:
{n,n+1} something like.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36719977
I'm not sure if I fully understand the requirement, but I'll take a stab at it  = )

See if this is what you're after:
List<string> list1 = new List<string>()
{
    "GO-1212a", "GO-1212b", "GO-1213a", "GO-1213b", "GO-1214a", "GO-1214b",
    "GO-1215a", "GO-1215b", "GO-1216a", "GO-1216b", "GO-1217a", "GO-1217b",
    "GO-1218a", "GO-1218b", "GO-1219a", "GO-1219b", "GO-1220a", "GO-1220b",
    "GO-1221a", "GO-1221b", "GO-1222a", "GO-1222b", "GO-1223a", "GO-1223b",
    "GO-1224a", "GO-1224b", "GO-1225a", "GO-1225b", "GO-1226a", "GO-1226b",
    "GO-1227a", "GO-1227b", "NA 7029a", "NA 7029b", "No DNAa", "No DNAb"
};

Dictionary<string, List<int>> categorized = new Dictionary<string, List<int>>();

for (int i = 0; i < list1.Count; i++)
{
    string key = list1[i].Substring(0, list1[i].Length - 1);

    if (!categorized.ContainsKey(key))
    {
        categorized.Add(key, new List<int>());
    }

    categorized[key].Add(i);
}

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to repeat the data 4 42
C# Windows app updating JSON file error. 9 48
Nested forach loop to linq 3 41
C# Get Current Exchange Rate from XE 9 40
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…
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!
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

739 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