1

次の正規表現を見てください。

(?:\(?")(.+)(?:"\)?)

この正規表現は、たとえば一致します

"a"
("a")

だけでなく、「a)

開始文字 [この場合は " または ) ] が終了文字と同じであるとどのように言えますか? これよりも簡単な解決策があるはずですよね?

"(.+)"|(?:\(")(.+)(?:"\))
4

3 に答える 3

1

特に正規表現でこれを行う良い方法があるとは思わないので、次のようなことをするのに行き詰まっています:

/(?:

"(.+)" 
|
\( (.+) \)

)/x
于 2013-05-13T10:34:28.843 に答える
1

どうですか:

(\(?)(")(.+)\2\1

説明:

(?-imsx:(\(?)(")(.+)\2\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):
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    \(?                      '(' (optional (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    "                        '"'
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  (                        group and capture to \3:
----------------------------------------------------------------------
    .+                       any character except \n (1 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )                        end of \3
----------------------------------------------------------------------
  \2                       what was matched by capture \2
----------------------------------------------------------------------
  \1                       what was matched by capture \1
----------------------------------------------------------------------
)                        end of grouping
于 2013-05-13T10:44:47.253 に答える
0

PHP ではプレースホルダーを使用できます。ただし、これは通常の正規表現の動作ではなく、PHP に特有のものであることに注意してください。

preg_match("/<([^>]+)>(.+)<\/\1>/")(\1最初の試合の結果を参照)

これは、最後の一致の条件として最初の一致を使用します。これは一致しますが、一致<a>something</a>しません<h2>something</a>

ただし、あなたの場合、最初のグループ内で一致した「(」を「)」に変える必要がありますが、これは機能しません。

更新: (and)<BRACE>ANDに置き換え<END_BRACE>ます。次に、を使用して一致させることができます/<([^>]+)>(.+)<END_\1>/。使用するすべての必須要素に対してこれを行います()[]{}<>

(a) is as nice as [f]preg_match_all を使用する<BRACE>a<END_BRACE> is as nice as <BRACKET>f<END_BRACKET>と、正規表現は両方をキャプチャします

$returnValue = preg_match_all('/<([^>]+)>(.+)<END_\\1>/', '<BRACE>a<END_BRACE> is as nice as <BRACKET>f<END_BRACKET>', $matches);

につながる

array (
  0 => 
  array (
    0 => '<BRACE>a<END_BRACE>',
    1 => '<BRACKET>f<END_BRACKET>',
  ),
  1 => 
  array (
    0 => 'BRACE',
    1 => 'BRACKET',
  ),
  2 => 
  array (
    0 => 'a',
    1 => 'f',
  ),
)
于 2013-05-13T10:47:08.163 に答える