techie
techie

Reputation: 11

Regex check previous line

Using regex (in c#.net) is it possible to check the previous line of a string?

For instance, I need to select strings in which the previous line is not a series of asterisks (prev line:******)

Upvotes: 1

Views: 2121

Answers (2)

Alan Moore
Alan Moore

Reputation: 75242

(?m)^(?<!^\*+\r?\n).+

(?m) turns on multiline mode so ^ can match the beginning of a line. The lookbehind checks the previous line; if it succeeds (that is, it doesn't see a line of asterisks), .+ consumes the current line.

Upvotes: 6

Joey
Joey

Reputation: 354614

You can use RegexOptions.MultiLine and then match something like the following:

(?<!^\*+$\r?\n?.*)foo

This matches "foo" only if the previous line doesn't consist of asterisks.

Upvotes: 1

Related Questions