RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex for a non-greedy match

/<(.+?)>/g

A quantifier followed by ? is lazy: it takes as little as it can and only grows when the rest of the pattern fails. Greedy .+ inside <...> swallows everything up to the last > in the line; .+? stops at the first.

What each part does

.+greedy — as many characters as possible
?made lazy — as few as possible
< … >the delimiters that bound each match

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
<a><b>matches<a>
<one>matches<one>
x<y>zmatches<y>
<>no match
no angle brackets hereno match

The mistake to avoid

Assuming lazy means fast. It is a different match, not a faster one — and on a failing input a lazy quantifier can backtrack just as badly.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers