使用正则表达式返回数组/列表

时间:2022-10-01 08:55:13

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

我想使用正则表达式返回数组字符串[]或列表List

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

我想比较一个字符串并返回以[和end with]开头的所有值。

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

我是regex的新手,所以请你解释一下你将用来产生正确结果的语法。

3 个解决方案

#1


3  

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

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

这个正则表达式\ [([^ \ [\]] *)\]意味着:

  1. \[ - character [
  2. \ [ - 字符[
  3. ([^\[\]]*) - any character, excluding [] any number of repetitions, group 1
  4. ([^ \ [\]] *) - 任何字符,不包括[]任意数量的重复,第1组
  5. \] - character ]
  6. \] - 字符]

Update:

更新:

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

#2


3  

string pattern = Regex.Escape("[") + "(.*?)]";
string input = "Test [Test2] .. sample text [Test3] ";

MatchCollection matches = Regex.Matches(input, pattern);
var myResultList = new List<string>();
foreach (Match match in matches)
{
    myResultList.Add(match.Value);
}

The result list would contain : [Test2], [Test3]

结果列表包含:[Test2],[Test3]

#3


1  

Stallman says : "When you want to solve a problem using regexp, now you have two problems".

Stallman说:“当你想用regexp解决问题时,现在你有两个问题”。

In your case it's something like ^\[.*\]$

在你的情况下,它就像^ \ [。* \] $

http://www.regular-expressions.info/dotnet.html

http://www.regular-expressions.info/dotnet.html

http://regexlib.com/CheatSheet.aspx?AspxAutoDetectCookieSupport=1

http://regexlib.com/CheatSheet.aspx?AspxAutoDetectCookieSupport=1

#1


3  

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

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

这个正则表达式\ [([^ \ [\]] *)\]意味着:

  1. \[ - character [
  2. \ [ - 字符[
  3. ([^\[\]]*) - any character, excluding [] any number of repetitions, group 1
  4. ([^ \ [\]] *) - 任何字符,不包括[]任意数量的重复,第1组
  5. \] - character ]
  6. \] - 字符]

Update:

更新:

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

#2


3  

string pattern = Regex.Escape("[") + "(.*?)]";
string input = "Test [Test2] .. sample text [Test3] ";

MatchCollection matches = Regex.Matches(input, pattern);
var myResultList = new List<string>();
foreach (Match match in matches)
{
    myResultList.Add(match.Value);
}

The result list would contain : [Test2], [Test3]

结果列表包含:[Test2],[Test3]

#3


1  

Stallman says : "When you want to solve a problem using regexp, now you have two problems".

Stallman说:“当你想用regexp解决问题时,现在你有两个问题”。

In your case it's something like ^\[.*\]$

在你的情况下,它就像^ \ [。* \] $

http://www.regular-expressions.info/dotnet.html

http://www.regular-expressions.info/dotnet.html

http://regexlib.com/CheatSheet.aspx?AspxAutoDetectCookieSupport=1

http://regexlib.com/CheatSheet.aspx?AspxAutoDetectCookieSupport=1