Regular Expressions 101

Community Patterns

Match XML that has a single root element

-2

Regular Expression
PCRE (PHP <7.3)

/
^\s*(?:<\?xml[^>]+>)?\s*<([^>]+?)>[\s\S]+?<\/\1>\s*?$
/

Description

Can be used as a basic validator to ensure that XML has only a single root element

Submitted by Jerome Wilson - 9 years ago