Sergey Dudik
Sergey Dudik

Reputation: 53

Regex new lines doesn't work

I want to get text between brackets. For example:


(a) As to money other than the purchase price payable to Seller at Closing, uncertified check of Purchaser up to the amount of $ {Uncertified Check Limit} ; and

(b) {All obligations affecting the Premises pursuant to the Administrative Code of the City of New York incurred prior to Closing and payable in money shall be discharged by Seller at or prior to Closing.}

Seller’s Representations.


From this text I want to get:

  1. {Uncertified Check Limit}

  2. {All obligations affecting the Premises pursuant to the Administrative Code of the City of New York incurred prior to Closing and payable in money shall be discharged by Seller at or prior to Closing.}

My current regex {(.*?)} doesn't work with new lines.
How should I modify it?

Upvotes: 2

Views: 80

Answers (2)

shA.t
shA.t

Reputation: 16958

I can suggest you to use a regex like {[^}]+} instead, and Regex works over new-lines fine!

var result = Regex.Matches(txt, @"{[^}]+}")
    .OfType<Match>().Select(c => c.Value).ToList();

[ C# Fiddle Demo ]

Upvotes: 1

Ulf Kristiansen
Ulf Kristiansen

Reputation: 1631

The dot explicitly matches any character except newline. Use RegexOptions.Singleline to change this behavior and make the dot match any character.

var regex = new Regex("{(.*?)}", RegexOptions.Singleline);

MSDN on RegexOptions.Singleline:

Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n).

The newlines in your example disappeared in html, hence the misunderstanding.

Upvotes: 1

Related Questions