XML Processing Instruction in PY
Match XML processing instructions like `<?xml version="1.0" encoding="utf-8"?>` or `<?xml-stylesheet href="..."?>`.
Try it in the PY tester →Pattern
regexPY
<\?[\w\-]+(?:\s+[\w\-]+\s*=\s*["'][^"']*["'])*\s*\?> (flags: g)Python (re) code
pyPython
import re
pattern = re.compile(r"<\\?[\\w\\-]+(?:\\s+[\\w\\-]+\\s*=\\s*[\"'][^\"']*[\"'])*\\s*\\?>")
input_text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
How the pattern works
<\? matches the literal opener. [\w\-]+ captures the PI target name. The repeating group matches optional `attr="value"` pairs. The trailing `\s*\?>` matches the closer. Works for the XML declaration and any custom processing instruction.
Examples
Input
<?xml version="1.0" encoding="UTF-8"?>Matches
<?xml version="1.0" encoding="UTF-8"?>
Input
<?xml-stylesheet type="text/xsl" href="style.xsl"?>Matches
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
Input
<root>plain</root>No match
—