Regex to Indent an XML File

前端 未结 7 1902
攒了一身酷
攒了一身酷 2020-12-21 06:14

Is it possible to write a REGEX (search replace) that when run on an XML string will output that XML string indented nicely?

If so whats the REGEX :)

7条回答
  •  死守一世寂寞
    2020-12-21 06:34

    I don't know if a regex, in isolation, could do a pretty-print format of an arbitrary XML input. You would need a regex being applied by a program to find a tag, locate the matching closing tags (if the tag is not self-closed), and so on. Using regex to solve this problem is really using the wrong tool for the job. The simplest possible way to pretty print XML is to use an XML parser, read it in, set appropriate serialization options, and then serialize the XML back out.

    Why do you want to use regex to solve this problem?

提交回复
热议问题