正则表达式结束标记=开始标记

时间:2013-05-13 10:27:04

标签: php regex

看看这个正则表达式:

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

这个正则表达式会匹配,例如

"a"
("a")

但也     “一个)

如何说起始字符[在本例中为“或”]与结束字符相同?必须有一个比这更简单的解决方案,对吗?

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

3 个答案:

答案 0 :(得分:1)

我不认为有一个很好的方法可以使用正则表达式进行此操作,所以你很难做到这样的事情:

/(?:

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

)/x

答案 1 :(得分: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

答案 2 :(得分:0)

您可以在PHP中使用占位符。但请注意,这不是正常的正则表达式行为,它对PHP特殊。:

preg_match("/<([^>]+)>(.+)<\/\1>/")\1引用第一场比赛的结果)

这将使用第一场比赛作为结束比赛的条件。这与<a>something</a>匹配,但与<h2>something</a>

匹配

但是在你的情况下,你需要把“(”在第一组中匹配成“)” - 这不会起作用。

更新:将()替换为<BRACE><END_BRACE>。然后,您可以使用/<([^>]+)>(.+)<END_\1>/进行匹配。对您使用的所有必需元素执行此操作:()[]{}<>和whatevs。

如果您使用preg_match_all

(a) is as nice as [f]将成为<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',
  ),
)