【发布时间】:2018-06-24 19:14:28
【问题描述】:
我有这样一句话:[amenities FREE HOT BREAKFAST AND WIRELESS INTERNET]
我想从方括号中提取子字符串FREE HOT BREAKFAST AND WIRELESS INTERNET。
我已经尝试过这个正则表达式\[(.*?)\],它给了我方括号内的整个子字符串。
我还尝试使用正则表达式 \bamenities\b 来获取“便利设施”一词,并尝试否定它周围的字符串。
但是,除了开头的子字符串 'Amenities' 之外,我想要其余的子字符串。我想提取方括号中所有可能的子字符串,前面是单词'Amenities'。
我正在尝试使用此代码在 C# 中提取以下内容。
public class Program
{
public static void Main(string[] args)
{
string s = "BEST AVAILABLE RATE , [amenities BREAKFAST] , [amenities WIFI] AND FITN ? - MORE IN HP";
MatchCollection m = Regex.Matches(s, @"\bamenities\b");
foreach(var item in m)
Console.WriteLine("{0}", item);
}
}
以下是预期
Input: BEST AVAILABLE RATE , [amenities BREAKFAST] , [amenities WIFI] AND FITN ? - MORE IN HP
Output:
string 1 - BREAKFAST
string 2 - WIFI
【问题讨论】:
-
为什么投反对票?
标签: c# regex string regex-negation regex-lookarounds