1

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

Eric
  • 95,302
  • 53
  • 242
  • 374
Alchemy
  • 59
  • 6

1 Answers1

2

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)*)
Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
  • 2
    Or use `[\s\S]` to match _any_ char. – Bart Kiers Jun 17 '11 at 11:47
  • Thanks Denis, I am working with the live code platform, to deploy on OSX. if i was to use the m modifier, where would i put that..? i tried: "(?<=\The cat)(.*)(?=\fire)/m" and "(?<=\The cat)(.*/m)(?=\fire)" without joy. – Alchemy Jun 17 '11 at 11:50
  • In php it would look like `/regex/s`. Not sure for Obj-C. But as suggested by Bart, try: `(?<=\The cat)([\s\S]*)(?=\fire)`. – Denis de Bernardy Jun 17 '11 at 11:52
  • I have another problem with that. It is to greedy and in the real working example, i am matching between the strings: "" and "". There are lots of instances of in the document, it matches everything between "" and the last instance of "". I just want it to look for the next instance of "". – Alchemy Jun 17 '11 at 11:59
  • 1
    You can make operators non-greedy by trailing them with a `?`: `([\s\S]*?)`. And you should *not* be [parsing html](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) using regex. – Denis de Bernardy Jun 17 '11 at 12:02
  • Hmm.. that post in the link is pretty explicit :-) .. Your last example actually works a treat in my static test. I was planning on pulling the file into a var and running the regex, and writing the file back. Can you give me an example why that may fail..? – Alchemy Jun 17 '11 at 12:16
  • If you've nested keys, it'll go bonkers. ` foo barbaz ` – Denis de Bernardy Jun 17 '11 at 12:18
  • Doesn't Ojb-C have a plist parser/reader that you can use as is? – Denis de Bernardy Jun 17 '11 at 12:18
  • Any way, this answer to my original question is good, and marked as answered. appreciated. – Alchemy Jun 17 '11 at 12:20
  • I will look into the plist parser, and see if live code has an api for that. Nested keys are not likely in this particular file. I have modified the file in as many ways as i can think of from within osx, and it always comes out with the same format. This way seems real easy, now i have the regex, but i will look for a more solid alternative. I do also have the luxury of expanding that regex a little to narrow down the chance of failure: "(?<=\AccountURL)((?:.|\s)*?)(?=\)". – Alchemy Jun 17 '11 at 12:26