Ryan
Ryan

Reputation: 867

How to make regex pattern lazy, or how to stop matching on first occurrence?

I'm trying to make my entire regex pattern lazy, instead of greedy, in C#. In PHP, there's the U option that makes the matching lazy. I don't see an equivalent setting in C#.

The pattern that I'm using is ^(.+)\(?([^)]+)\)(.*)$, with the following example data ...

 BILYARD    (2): Reviewed data.
BHAT-A (1): Reviewed data.
BEEL/RUBIN (1): Emails with engagement (haha)team and OGC.  Reviewed data.          
 BILLY BOB (1hr) Mwahaha

If we look at how this works on regex101.com, we can see the matches: https://regex101.com/r/pT4tM5/3

That third line matches up to the 2nd set of parenthesis, instead of stopping at the first set. How can I make this lazy, or just stop matching at the first set of parens?

Upvotes: 1

Views: 328

Answers (1)

vks
vks

Reputation: 67968

^(.+?)\(([^)]+)\)(.*)$

   ^^

Here.See demo.

https://regex101.com/r/pT4tM5/4

Upvotes: 3

Related Questions