Search code examples
regexxmlmultiline

How can I make this multiline regex matching pattern non greedy?


I have the following string:

<SomeXmlProperty att="abc" 
att2="def"
att3="ghi"/>
<SomeXmlProperty att4="jkl" 
att5="mno"
att6="pqr"><SomeXmlProperty>
<SomeXmlProperty att="stu" 
att3="vwx"
att6="yz"   
/>

I am needing to match the elements with self closing tags. I have tried using both <SomeXmlProperty((.|\n|\r)*)\/> and <SomeXmlProperty((.|\n|\r)*?)\/> which get close but the middle element still gets matched. What am I doing wrong here?


Solution

  • You can use a character class that excludes <s instead to avoid matching multiple tags:

    <SomeXmlProperty[^<]*\/>
    

    Demo: https://regex101.com/r/VEyVg1/1