I want to capture a whole line, and, optionally a ID, with the regex pattern H-\d{4}
, like H-1234
.
These are two sample lines, one with ID, the other without ID:
Sample line with H-5722 id
Sample line without id
In the first ALL should capture be the whole line, and ID H-5722. In the second ALL should capture the whole line, and ID should be empty.
This regex work for the first line, capturing ALL and ID:
^(?<ALL>.*?(?<ID>H-\d{4})\b.*)$
but it doesn't match the second line, as expected, because it doesn't have an ID.
So, I've tried to make the ID capture optional with a non-capturing group with ?
zero-or-one modifier (?:(?<ID>H-\d{4}))?
, or modified the ID group so that it can capture the expression or an empty string (?<ID>H-\d{4}|)
:
^(?<ALL>.*?(?:(?<ID>H-\d{4})\b)?.*)$
^(?<ALL>.*?(?<ID>H-\d{4}|)\b.*)$
With these modifications ALL capture the whole lines in both examples. But it doesn't capture the ID.
How can I achieve this?
I'm using .NET regex implementation, but I think it's very similar to other implementations.