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]。これは、行ごとに 1 つの可能なキャプチャを想定しています。さらに必要な場合は、 を使用して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 に答える