.net正则表达式匹配行 [英] .net regex match line

查看:113
本文介绍了.net正则表达式匹配行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Why does ^.*$ does not match a line in:


这是一些示例文本

This is some sample text

这是另一行

这是第三行

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

how can I create a regular expression that will match an entire line so that when finding the next match it will return me the next line.

换句话说,我想拥有一个正则表达式,以便第一个匹配项= 这是一些示例文本,下一个匹配项= 这是另一行等...

In other words I will like to have a regex so that the first match = This is some sample text , next match = this is another line etc...

推荐答案

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

^ and $ match on the entire input sequence. You need to use the Multiline Regex option to match individual lines within the text.

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

请参见此处,用于解释regex选项。关于多行选项的说明如下:

See here for an explanation of the regex options. Here's what it says about the Multiline option:


多行模式。更改^和$的含义,以便它们分别在任意行的
开头和结尾处匹配,而不仅仅是整个字符串的
开头和结尾。

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

这篇关于.net正则表达式匹配行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆