Click here to Skip to main content
15,895,812 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi everybody, I got a problem with regex, hope someone can help me.

I have a list of strings and each string contains uppercase word of 17 letters, I need to get this word from string.
Something like this:
C#
string pattern = @"[A-Z0-9]{17}";
Regex regex = new Regex(pattern);
List<string> result = new List<string>();
foreach (string val in list)
{
    Match match = regex.Match(pattern);
    result.Add(match.Value);
}
Posted
Updated 22-Dec-11 12:43pm
v2
Comments
pavel.bidenko 22-Dec-11 15:45pm    
Still can't get it to work, it's not laziness, just still can't learn regex((
Monjurul Habib 22-Dec-11 16:03pm    
what is your problem now ??
Monjurul Habib 22-Dec-11 16:13pm    
review my updated answer.
pavel.bidenko 22-Dec-11 16:26pm    
It's just too late for my brain, the regex for this is just simple '[A-Z0-9]{17}', but I got a mistake in a code snippet 'Match match = regex.Match(pattern);' Weird to compare pattern with pattern, isn't it?)

I tried and its works:

C#
   List<string> list = new List<string>();
   list.Add("ASD");
   list.Add("ASFsdsd");
   list.Add("adsASFsdsd");

   string pattern = @"[A-Z]{3}";
   Regex regex = new Regex(pattern);
   List<string> result = new List<string>();

   foreach (string val in list)
   {
       Match match = regex.Match(val);
       result.Add(match.Value);
   }

/*Result will be list with elements:
  ASD
  ASF
  ASF
*/


If you replace 3 with 17 it must work for you.
 
Share this answer
 
v2
Comments
pavel.bidenko 23-Dec-11 3:22am    
Thanks, thats it
Try following:
C# Regex.Split Method Examples

[Updated]
If you are looking for more advanced Regular Expression, follow the below links:
Regular Expressions Cheat Sheet
Learn Regular Expression (Regex) syntax with C# and .NET
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900