Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个看起来像这样的令牌#foo bar#
#foo bar#
我想做的是使用正则表达式做两件事,
bar
到目前为止,我正在使用它来获取整个令牌作为一个组(?<group1>#foo [^#]+#)
(?<group1>#foo [^#]+#)
我很困惑如何bar成为第二组?
我正在尝试这种变化,但还没有完全实现,我们将不胜感激。
(?<group1>#foo (?<group2[^#]+)#)
我对你的正则表达式做了一个小改动,并想出了这个:
(?<group1>#foo (?<group2>[^#]+)#)
示例可以在这里找到:http ://regexr.com?33api
你实际上非常接近。我让它与这个正则表达式一起工作:
(您在第 2 组之后错过了结束>)。
>
该程序输出bar:
var input = "#foo bar#"; var m = Regex.Match(input, "(?<group1>#foo (?<group2>[^#]+)#)"); Console.WriteLine(m.Groups["group2"].Value);