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

Regex matching a group that may or may not exist on string

I have a text which contains somewhere in the middle the following strings:

  1. Sat28 B158 RGX 1100 1200
  2. Sat28 Hoover 0005 RGX B158 RGX 1100 1200

I want to capture only the following groups:

  1. Sat28 B158 RGX 1100 1200
  2. Sat28 B158 RGX 1100 1200

To find the first option I’ve come up with the following regex:

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

(\w{3}\d{2})\s+([B]\d{3})\s+(\w{3})\s+(\d{4})\s+(\d{4})

Example: https://regexr.com/772aq
(Only the first option is included within the text)

However, when trying to find the second option as well, I’ve added a non-capturing group, which in turn skips the first option:

(\w{3}\d{2})\s+(?:hoover\s+\d{4}\s+RGX\s+)(\[B]\d{3})\s+(\w{3})\s+(\d{4})\s+(\d{4})

Example: https://regexr.com/772at
(Both options are included within the text, but only match the second option)

Basically, I added a non-capturing group (?:) that looks (and should ignore) the word hoover, blank space, 4 digits, blank space, the word RGX, blank space.

Any help is appreciated. Thanks!

Edit: text added to the regexr.com’s examples for clarification.

>Solution :

As I see you should just make the group optional (\w{3}\d{2})\s+(?:hoover\s+\d{4}\s+RGX\s+)?([B]\d{3})\s+(\w{3})\s+(\d{4})\s+(\d{4}). Also not quite sure why you have escaped the [ just before the B but maybe it was a typo.

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