Alchemy
Alchemy

Reputation: 59

Multi line Reg Exp match

As an example take this chunk:

The cat sat on the
mat, and the dog
could not get his place in

front of the fire.

I have an expression to match a string between two strings, and return just the contents between:

put "(?<=\The cat)(.*)(?=\the)" into myreg

returns: cat sat on

How can i expand this to match across multiple lines..? To obtain this from the code:

put "(?<=\The cat)(.*)(?=\fire)" into myreg

so i want:

cat sat on the mat, and the dog could not get his place in front of the

Upvotes: 1

Views: 245

Answers (1)

Denis de Bernardy
Denis de Bernardy

Reputation: 78571

The . in a regex will match anything except a new line. There is a modifier (the specific on depends on the platform) to make it multiline. It typically is one of s, m or n.

Alternatively, replace (.*) with something like:

((?:.|\s)*)

Upvotes: 2

Related Questions