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.
| Input | Result | Matched |
|---|---|---|
| <a><b> | matches | <a> |
| <one> | matches | <one> |
| x<y>z | matches | <y> |
| <> | no match | — |
| no angle brackets here | no 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 RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID