For example we have following string:
Something
AnotherThing
Something AnotherThing
If I use RegexOptions.Singleline
with pattern Something.+?AnotherThing
then I get two matches when I want to match first and second lines only. I want to use something like FirstLine#endofline##startofline#AnotherLine
. So i use:
var regex = new Regex(@"Something$^AnotherThing", RegexOptions.Multiline);
but it doesn't work. I know that I can use some hack with Singleline to match first two lines (and not the last one), but the question: Is it even possible to match exact two texts in exact 2 lines without Singleline specifier, with Multiline option only? And why does it behaves like this.
How about:
Something\r?\nAnotherThing
\\r?
in case the string doesn't come from Windows.
The reason Something$^AnotherThing
doesn't work with the RegexOptions.Multiline
option, is because ^
and $
match at line breaks, not the line breaks themselves, so the following would work:
new Regex(@"Something$\r?\n^AnotherThing", RegexOptions.Multiline);
Try to match using the carriage return and line break characters, eg
Something\r?\nAnotherThing
Basically, carriage returns are causing you trouble (you're not alone). Do you know which OS your text is coming from? If it's from Windows then there will be a \\r before the \\n, which you need to account for.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.