Skip to main content
 首页 » 编程设计

c#之如何在 C# 中匹配和捕获正则表达式

2024年01月29日26虾米哥

我有一个像这样的正则表达式

something (.*) someotherthing 

如何使用 Regex 对象来匹配整个表达式并获取捕获的值?

请您参考如下方法:

这是我认为您要求的示例:

Match m = Regex.Match( "something some stuff in the middle someotherthing",  
         "something (.*) someotherthing" ); 
if ( m.Success ) 
   Console.WriteLine( "groups={0}, entire match={1}, first group={2}",  
                      m.Groups.Count, m.Groups[0].Value,  
                      m.Groups[1].Value );