Skip to main content
 首页 » 编程设计

c#之正则表达式 C# 如何

2024年02月13日28zhoujg

static void Main(string[] args) 
        { 
            int count = 0; 
            String s = "writeln('Helloa a') tung ('main')"; 
            String patern = @"\'+[\S+\s]*\'"; 
            Regex myRegex = new Regex(patern); 
            foreach (Match regex in myRegex.Matches(s)) { 
                Console.WriteLine(regex.Value.ToString()); 
            } 
 
        } 

运行时显示

'Helloa a') tung ('main' 

我不想这样

我要打印

'Helloa a' 
'main' 

你能帮帮我吗?

请您参考如下方法:

尝试使用这个正则表达式:

@"\'[^']+\'" 

它将打印:

'Helloa a' 
'main'