1

看看这个正则表达式:

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

这个正则表达式将匹配例如

"a"
("a")

还有“一)

我怎么能说起始字符 [ 在这种情况下 " 或 ) ] 与结束字符相同?肯定有比这更简单的解决方案,对吧?

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

3 回答 3

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 回答
1

我认为没有专门使用正则表达式的好方法,所以你被困在做这样的事情:

/(?:

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

)/x
于 2013-05-13T10:34:28.843 回答
0

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

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

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

但是,在您的情况下,您需要将第一组中匹配的“(”转换为“)” - 这不起作用。

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

(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 回答