python - 如何正则匹配成对标签对中的内容?
PHP中文网
PHP中文网 2017-04-17 15:52:15
0
3
912
PHP中文网
PHP中文网

认证0级讲师

reply all(3)
黄舟

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

阿神

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.

迷茫

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!