C#正则表达式查找大写单词 [英] C# Regex to find uppercase word

查看:101
本文介绍了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);
}

推荐答案

我尝试过,其工作原理是:

I tried and its works:

   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#Regex.Split方法示例

[更新]
如果您正在寻找更高级的正则表达式,请访问以下链接:
正则表达式速查表
通过C#和.NET学习正则表达式(Regex)语法
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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆