返回使用正则表达式一个数组/列表 [英] Return a array/list using regex

查看:238
本文介绍了返回使用正则表达式一个数组/列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想返回数组的String [] 或列表名单,其中,串>使用 正则表达式

我想比较一个字符串键,返回与开头的所有值[和<$ C $结束C>] 。

我是新来的正则表达式,所以请你解释一下,你将使用产生正确的结果的语法。

解决方案

  VAR的结果= Regex.Matches(输入,@\ [([^ \ [\] *)\ ])
    .Cast&LT;匹配&GT;()
    。选择(米=&GT; m.Groups [1]。价值).ToArray();
 

这正则表达式 \ [([^ \ [\] *)\] 表示:

  1. \ [ - 字符 [
  2. ([^ \ [\] *) - 任何字符,不包括 [] 任意数量的重复,1组
  3. \] - 字符]

更新

  VAR的结果= Regex.Matches(输入,@\ [^ \ [\] * \])
    .Cast&LT;匹配&GT;()
    。选择(米=&GT; m.Value).ToArray();
 

I would like to return a array string[] or a list List<string> using regex.

I want to compare a string and return all values that starts with [ and end with ].

I am new to regex, so would you please explain the syntax that you will be using to produce the right results.

解决方案

var result = Regex.Matches(input, @"\[([^\[\]]*)\]")
    .Cast<Match>()
    .Select(m => m.Groups[1].Value).ToArray();

This regex \[([^\[\]]*)\] means:

  1. \[ - character [
  2. ([^\[\]]*) - any character, excluding [] any number of repetitions, group 1
  3. \] - character ]

Update:

var result = Regex.Matches(input, @"\[[^\[\]]*\]")
    .Cast<Match>()
    .Select(m => m.Value).ToArray();

这篇关于返回使用正则表达式一个数组/列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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