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


regular expression end tag = start tag

看一下这个正则表达式:

(?:'(?")(.+)(?:"')?)

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

"a"
("a")

但也"一个)

我怎么能说开始字符[在这个例子中是" or ")]和结束字符是相同的?肯定有比这更简单的解决方案,对吧?

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

我不认为有一个很好的方法来做到这一点,特别是与regex,所以你被困在这样做:

/(?:
"(.+)" 
|
'( (.+) ')
)/x

如何:

('(?)(")(.+)'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

您可以在PHP中使用占位符。但请注意,这不是普通的Regex行为,这是PHP特有的。

preg_match("/<([^>]+)>(.+)<'/'1>/") ('1引用第一次匹配的结果)

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

但是在您的情况下,您需要将第一组中匹配的"("转换为")"-这是行不通的

更新:将()替换为<BRACE><END_BRACE>。然后可以使用/<([^>]+)>(.+)<END_'1>/进行匹配。对你使用的所有Required元素都这样做:()[]{}<>和其他。

如果您使用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',
  ),
)