Regular Expressions 101

Community Patterns

Community Library Entry

-3

Regular Expression
PCRE (PHP <7.3)

/
<([^\/> ]+)\s*([^>]*)\s*(?:\/>|>(.*)<\/\1>)
/
g

Description

<([^/> ]+)\s*([^>])\s(?:/>|>(.*)</\1>) - (with global) finds all tags in scope

1st group is tagname 2nd group is attributes 3rd group (if it exists) is value of the tag

Apply pattern again on 3th group to find internal tags (recurse in the code to parse entire xml!)

Apply pattern below on 2nd group to find all attributes:

\s+([^<>=" ]+)\s*=\s*"([^"]*)" - (with global) finds attributes

1st group is attributename
2nd group is attributevalue
Submitted by veryanonymous - 8 years ago