Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Optional string with a required string in Regex

I need to detect class of strings which must include X, but may have Y optionally.

<<TAG##X#123>>
<<TAG##X#123##Y#456>>

So here’s the regex I tried, greps x but can’t detect y value:

(<<)(TAG##X#(?<x>.+)(##Y#(?<y>.+))?)(>>)

What am I missing?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

(?<x>.+) matches the whole 123##Y#456 already.

Add a ? after the quantifier, to make this ungreedy: (?<x>.+?)

https://regex101.com/r/UuT50e/1

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading