Link to home
Start Free TrialLog in
Avatar of zhshqzyc
zhshqzyc

asked on

Create key and pair

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

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

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
Avatar of zhshqzyc
zhshqzyc

ASKER

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

I don't understand where the regular expression part is supposed to come into play. Can you elaborate?
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.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial