如何使用Regex在c#中以字符串形式提取方括号的内容?

时间:2022-09-13 07:48:03

if i have a string of text like below, how can i collect the contents of the brackets in a collection in c# even if it goes over line breaks?

如果我有如下所示的文本字符串,那么如何在c#中收集括号中的内容,即使它经过换行符?

eg...

如……

string s = "test [4df] test [5yu] test [6nf]";

should give me..

应该给我. .

collection[0] = 4df

[0]= 4 df集合

collection[1] = 5yu

集合[1]= 5

collection[2] = 6nf

[2]= 6 nf集合

4 个解决方案

#1


16  

You can do this with regular expressions, and a bit of Linq.

您可以使用正则表达式和一点Linq来实现这一点。

    string s = "test [4df] test [5y" + Environment.NewLine + "u] test [6nf]";

    ICollection<string> matches =
        Regex.Matches(s.Replace(Environment.NewLine, ""), @"\[([^]]*)\]")
            .Cast<Match>()
            .Select(x => x.Groups[1].Value)
            .ToList();

    foreach (string match in matches)
        Console.WriteLine(match);

Output:

输出:

4df
5yu
6nf

Here's what the regular expression means:

正则表达式的意思是:

\[   : Match a literal [
(    : Start a new group, match.Groups[1]
[^]] : Match any character except ]
*    : 0 or more of the above
)    : Close the group
\]   : Literal ]

#2


3  

Regex regex = new Regex(@"\[[^\]]+\]", RegexOptions.Multiline);

#3


1  

The key is to correctly escape the special characters used in regular expressions, for example you can match a [ character this way: @"\["

关键是要正确地转义正则表达式中使用的特殊字符,例如,您可以通过以下方式匹配一个[字符:@"\ " ["

#4


0  

Regex rx = new Regex(@"\[.+?\]");
var collection = rx.Matches(s);

You will need to trim the square brackets off, the important part is the lazy operator.

你需要把方括号修剪掉,最重要的部分是惰性操作符。

#1


16  

You can do this with regular expressions, and a bit of Linq.

您可以使用正则表达式和一点Linq来实现这一点。

    string s = "test [4df] test [5y" + Environment.NewLine + "u] test [6nf]";

    ICollection<string> matches =
        Regex.Matches(s.Replace(Environment.NewLine, ""), @"\[([^]]*)\]")
            .Cast<Match>()
            .Select(x => x.Groups[1].Value)
            .ToList();

    foreach (string match in matches)
        Console.WriteLine(match);

Output:

输出:

4df
5yu
6nf

Here's what the regular expression means:

正则表达式的意思是:

\[   : Match a literal [
(    : Start a new group, match.Groups[1]
[^]] : Match any character except ]
*    : 0 or more of the above
)    : Close the group
\]   : Literal ]

#2


3  

Regex regex = new Regex(@"\[[^\]]+\]", RegexOptions.Multiline);

#3


1  

The key is to correctly escape the special characters used in regular expressions, for example you can match a [ character this way: @"\["

关键是要正确地转义正则表达式中使用的特殊字符,例如,您可以通过以下方式匹配一个[字符:@"\ " ["

#4


0  

Regex rx = new Regex(@"\[.+?\]");
var collection = rx.Matches(s);

You will need to trim the square brackets off, the important part is the lazy operator.

你需要把方括号修剪掉,最重要的部分是惰性操作符。