Reputation: 36048
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...
Upvotes: 5
Views: 6441
Reputation: 7966
^
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.
Upvotes: 10
Reputation: 31184
use regex options
Regex regex = new Regex("^.*$", RegexOptions.Multiline);
Upvotes: 4
Reputation: 56809
You have to enable RegexOptions.Multiline
to make ^
and $
matches the start and end of line. Otherwise, ^
and $
will match the start and end of the whole input string.
Upvotes: 2