本文介绍了C# Regex Split - 方括号内的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在尝试在 C#(最新的 .NET 和 Visual Studio 2008)中拆分一个字符串,以便检索方括号内的所有内容并丢弃剩余的文本.
I'm currently trying to split a string in C# (latest .NET and Visual Studio 2008), in order to retrieve everything that's inside square brackets and discard the remaining text.
例如:H1 受体拮抗剂 [HSA:3269] [PATH:hsa04080(3269)]"
在这种情况下,我有兴趣将HSA:3269"和PATH:hsa04080(3269)"放入一个字符串数组中.
In this case, I'm interested in getting "HSA:3269" and "PATH:hsa04080(3269)" into an array of strings.
如何实现?
推荐答案
Split
在这里帮不了你;你需要使用正则表达式:
Split
won't help you here; you need to use regular expressions:
// using System.Text.RegularExpressions;
// pattern = any number of arbitrary characters between square brackets.
var pattern = @"[(.*?)]";
var query = "H1-receptor antagonist [HSA:3269] [PATH:hsa04080(3269)]";
var matches = Regex.Matches(query, pattern);
foreach (Match m in matches) {
Console.WriteLine(m.Groups[1]);
}
产生你的结果.
这篇关于C# Regex Split - 方括号内的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!