简体   繁体   中英

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?

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

   ^^

Here.See demo.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM