It looks like you are looking for the ? quantifier (6th down in the list in the docs). It will allow the trailing portion to appear once or not at all, covering both cases:
^Content-Type:\s+([^;]+)(?:;.*)?
Here are the changes I would recommend:
- Do not capture
. in your capture group. * is greedy, so you will get undesirable characters sometimes: e.g. if you have two semicolons in the string, the first one will get captured. Instead, capture [^;], which means "anything but semicolons".
- Change the quantifier in the main catpure group from
* to +. You want at least one character to match, which is what + expresses.
- I would also add the
+ quantifier to the preceding \s just to be safe. It will allow you to match multiple spaces, should that ever happen.
- Make the part that matches the
; into a non-capturing group (a group starting with (?:. This allows you to apply the ? quantifier to it.
As @RudyTheHunter indirectly points out, if you use plain re.match, you don't need the leading ^ or the trailing portion after the semicolon at all since match looks in the beginning of the string.
You can therefore use just
Content-Type:\s+([^;]+)
^Content-Type:\s+(.*?)(?=>;|$)although you don't need regex at all for such a simple case.