gpt4 book ai didi

c# - .net 正则表达式匹配行

转载 作者:太空狗 更新时间:2023-10-29 22:11:00 24 4
gpt4 key购买 nike

为什么 ^.*$ 与以下行不匹配:

This is some sample text

this is another line

this is the third line

我如何创建一个匹配整行的正则表达式,以便在找到下一个匹配项时返回下一行。

换句话说,我想要一个正则表达式,这样第一个匹配项 = This is some sample text ,下一个匹配项 = this is another line 等...

最佳答案

^$ 匹配整个输入序列。您需要使用多行正则表达式选项来匹配文本中的各个行。

Regex rgMatchLines = new Regex ( @"^.*$", RegexOptions.Multiline);

参见 here有关正则表达式选项的解释。以下是关于多行选项的说明:

Multiline mode. Changes the meaning of ^ and $ so they match at thebeginning and end, respectively, of any line, and not just thebeginning and end of the entire string.

关于c# - .net 正则表达式匹配行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11472250/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com