the "?" makes the regex non greedy: Example 2: Non-Greedy: Regex: <h[0-9]>.*?</h[0-9]> Input: <h3>My Title</h3> <h[0-9]> matches: <h3> .*? matches: My Title </h[0-9]> matches: </h3>