Real Dreams
Real Dreams

Reputation: 18038

Mulitline RegEx

Consider following text:

$content=<<<EOT
    {
        "translatorID": "f4a5876a-3e53-40e2-9032-d99a30d7a6fc",
        "label": "ACL",
        "creator": "Nathan Schneider",
        "target": "^https?://(www[.])?aclweb\\.org/anthology-new/[^#]+",
        "minVersion": "1.0.7",
        "maxVersion": "",
        "priority": 100,
        "browserSupport": "gcs",
        "inRepository": true,
        "translatorType": 4,
        "lastUpdated": "2012-01-01 01:42:16"
    }

    // based on ACM translator
    function detectWeb(doc, url) {
      var namespace = doc.documentElement.namespaceURI;
        var nsResolver = namespace ? function(prefix) {
            if (prefix == 'x') return prefix; else return null;
        } : namespace;

        var bibXpath = "//a[./text() = 'bib']"
        if(doc.evaluate(bibXpath, doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()) {
          return "multiple"
        }
      //commenting out single stuff
      // if (url.indexOf("/anthology-new/J/")>-1)
      //  return "journalArticle";
      // else
      //  return "conferencePaper";
    }
EOT;

I want select text between { and } in the beginning of text. I test following but it did not yield desired text.

preg_match('~\{(.*)\}~m',$content,$meta);
var_dump( $meta);

What is wrong?

Upvotes: 0

Views: 64

Answers (4)

Ciro
Ciro

Reputation: 662

try

preg_match('~\{(.*)\}~m',$content,$meta,PCRE_MULTILINE);

Adittional documention http://php.net/manual/en/reference.pcre.pattern.modifiers.php

Upvotes: 0

Petah
Petah

Reputation: 46060

This is probably what you are after:

preg_match('/\{(.*?)\}/s', $string, $result);

Upvotes: 1

hakre
hakre

Reputation: 198237

Even in multiline mode, . does not match a newline. You can make it match a newline by using the s (PCRE_DOTALL) modifier:

preg_match('~\{(.*)\}~sm',$content,$meta);
                      ^

However your case also requires to make the match non-greedy otherwise this will pick from the source-code below as well:

preg_match('~\{(.*?)\}~sm',$content,$meta);
                  ^

Demo

Upvotes: 2

biziclop
biziclop

Reputation: 14626

Cheat sheet http://www.cs.washington.edu/education/courses/cse190m/11su/cheat-sheets/php-regex-cheat-sheet.pdf says:

 Base Character Classes
 .  (Period) – Any character except newline

But it also says

Pattern Modifiers
s   Dotall - . class includes newline

Upvotes: 1

Related Questions