0

考虑以下文本:

$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;

我想在文本的开头{和之间选择文本。}我测试以下但它没有产生所需的文本。

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

怎么了?

4

4 回答 4

2

即使在多行模式下,.也不匹配换行符。s您可以使用(PCRE_DOTALL) 修饰符使其与换行符匹配:

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

但是,您的情况还需要使匹配不贪婪,否则这也将从下面的源代码中选择:

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

演示

于 2012-06-19T10:17:20.673 回答
1

备忘单http://www.cs.washington.edu/education/courses/cse190m/11su/cheat-sheets/php-regex-cheat-sheet.pdf说:

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

但它也说

Pattern Modifiers
s   Dotall - . class includes newline
于 2012-06-19T10:13:51.493 回答
1

这可能是你所追求的:

preg_match('/\{(.*?)\}/s', $string, $result);
于 2012-06-19T10:18:42.730 回答
0

尝试

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

附加文档 http://php.net/manual/en/reference.pcre.pattern.modifiers.php

于 2012-06-19T10:22:38.590 回答