0

I have a regular expression to search for complete h1 tags (from start of <h1> to end of </h1>) in a content but it searches for the whole content

Regular expression

<h1.*<\/h1>

Content to search for

<h1 style=""></h1><span> alsjd alkj dalsd a</span><h1>asdsadsa</h1>

The result is one match only from first occurrence of starting h1 to last occurrence of ending h1. It matches the whole content.

I want it to give me 2 occurrence of starting and ending h1 tags like the result should be

<h1 style=""></h1> and 
<h1>asdsadsa</h1>

Surinder
  • 415
  • 1
  • 4
  • 16
  • 1
    You should specify the context, which parser are you using? From what you describe it seems the case of greedy matching (maximal munch), try `?` for lazy matching [reference](https://learn.microsoft.com/en-us/dotnet/standard/base-types/quantifiers-in-regular-expressions?redirectedfrom=MSDN) – Jiraiya Jun 22 '23 at 05:21

1 Answers1

1

I think you've to make .* non greedy

use this

<h1.*?<\/h1>
rawat0419
  • 136
  • 9