1

我正在研究一个正则表达式,但我无法修复它。

我正在使用 PHP 扫描文档 (.php),我正在寻找:$__this('[TEXT]')$__this("[TEXT]")

所以我的问题是:有人可以帮助我使用在字符串中搜索的正则表达式:$__this('[TEXT]')$__this("[TEXT]")并给我[TEXT]

更新(有答案,感谢@Explosion Pills):

$string = '$__this("Foo Bar<br>HelloHello")';
preg_match('/\$__this\(([\'"])(.*?)\1\)/xi', $string, $matches);
print_r($matches);
4

3 回答 3

2
preg_match('/
    \$__this # just $__this.  $ is meta character and must be escaped
    \(       # open paren also must be escaped
    ([\'"])  # open quote (capture for later use).  \' is needed in string
    (\[      # start capture.  open bracket must also be escaped
    .*?      # Ungreedily capture whatever is between the quotes
    \])      # close the open bracket and end capture
    \1       # close the quote (captured earlier)
    \)       # close the parentheses
/xi'         # ignore whitespace in pattern, allow comments, case insensitive
, $document, $matches);

捕获的文本将在$matches[2]. 这假设每行可能捕获一次。如果您需要更多,请使用preg_match_all.

于 2013-02-01T14:17:14.467 回答
0

怎么样:

preg_match('/\$__this(?:(\'|")\((.+?)\)\1)/', $string);

解释:

(?-imsx:\$__this(?:(\'|")\((.+?)\)\1))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  \$                       '$'
----------------------------------------------------------------------
  __this                   '__this'
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    (                        group and capture to \1:
----------------------------------------------------------------------
      \'                       '''
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      "                        '"'
----------------------------------------------------------------------
    )                        end of \1
----------------------------------------------------------------------
    \(                       '('
----------------------------------------------------------------------
    (                        group and capture to \2:
----------------------------------------------------------------------
      .+?                      any character except \n (1 or more
                               times (matching the least amount
                               possible))
----------------------------------------------------------------------
    )                        end of \2
----------------------------------------------------------------------
    \)                       ')'
----------------------------------------------------------------------
    \1                       what was matched by capture \1
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2013-02-01T14:16:30.013 回答
0

这是一个解决方案,它也可以捕获带有引号和撇号的字符串。

$txt = "
blah blah blah
blah \$_this('abc') blah
blah \$_this('a\"b\"c') blah balah \$_this('a\"b\"c\'')
\$_this(\"123\");\$_this(\"1'23\") \$_this(\"1'23\\\"\")
";

  $matches = array();
  preg_match_all('/(?:\$_this\()(?:[\'"])(.*?[^\\\])(?:[\'"])(?:\))/im', $txt, $matches);
  print_r($matches[1]);
于 2013-02-01T14:37:22.257 回答