search

Home  >  Q&A  >  body text

python - 如何正则匹配成对标签对中的内容?

PHP中文网PHP中文网2804 days ago937

reply all(3)I'll reply

  • 黄舟

    黄舟2017-04-17 15:54:15

    I think you want to match pairs of brackets at any level, right? Simply speaking, it is not feasible to use regex.

    A regular expression is essentially a finite state machine. A finite state machine does not store anything other than the current state it is in.
    In terms of matching pairs of brackets at any level, storage of arbitrary length (corresponding to the level) is required. Hard-coded regular expressions cannot do this, and libraries that solve this problem also require dynamic memory. See: http://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns

    If your problem is more general, such as any pair of labels. The most common ones are XML or HTML. Due to the diversity and compatibility of syntax, this will be more difficult in practice. You basically have to use a library. See: http://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-reg

    reply
    0
  • 阿神

    阿神2017-04-17 15:54:15

    The regular engine in the standard library is not supported. This regex library supports it. You can find the usage by searching for "recursive" on the web page.

    reply
    0
  • 迷茫

    迷茫2017-04-17 15:54:15

    The correct solution on the first floor can be achieved using a stack.

    reply
    0
  • Cancelreply