本文介绍了C#正则表达式查找大写单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我遇到了正则表达式问题,希望有人可以帮助我.

我有一个字符串列表,每个字符串包含17个字母的大写单词,我需要从字符串中获取这个单词.
像这样的东西:

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:

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);
}

推荐答案

   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
*/



如果将3替换为17,它必须对您有用.



If you replace 3 with 17 it must work for you.



这篇关于C#正则表达式查找大写单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:09
查看更多