Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net regex match line

Tags:

c#

.net

regex

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

This is some sample text

this is another line

this is the third line

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...

like image 673
Tono Nam Avatar asked Jul 13 '12 14:07

Tono Nam


Video Answer


1 Answers

^ 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);

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.

like image 107
TLiebe Avatar answered Sep 30 '22 13:09

TLiebe