Regex: To pull out a sub-string between two tags in a string

后端 未结 9 1973
情书的邮戳
情书的邮戳 2020-12-08 00:35

I have a file in the following format:

Data Data
Data
[Start]
Data I want
[End]
Data

I\'d like to grab the Data I want from between the

9条回答
  •  心在旅途
    2020-12-08 00:46

    While you can use a regular expression to parse the data between opening and closing tags, you need to think long and hard as to whether this is a path you want to go down. The reason for it is the potential of tags to nest: if nesting tags could ever happen or may ever happen, the language is said to no longer be regular, and regular expressions cease to be the proper tool for parsing it.

    Many regular expression implementations, such as PCRE or perl's regular expressions, support backtracking which can be used to achieve this rough effect. But PCRE (unlike perl) doesn't support unlimited backtracking, and this can actually cause things to break in weird ways as soon as you have too many tags.

    There's a very commonly cited blog post that discusses this more, http://kore-nordmann.de/blog/do_NOT_parse_using_regexp.html (google for it and check the cache currently, they seem to be having some downtime)

提交回复
热议问题