Search code examples
c#regex

How do I match all four cases in Regex?


I'm very new to Regex and I've been tinkering around for 3 hours now and I'm close but just can't get it.

I have this regex (that I'll use in c# later on, if that's important):

To:\s*[^<]+(?<from><?\s*(sip:[^> ]+\s*)>?\s*(?:;tag=([^\r\n;]+))?)\s*

and I'm trying to get two groups out of the following possible inputs:

To: <sip:[email protected]:5080>;tag=myTag
To: <sip:[email protected]:5080>
To: sip:[email protected]:5080
To: sip:[email protected]:5080;tag=myTag

What I expect

The Group2 is optional, as there isn't always a tag.

Current status

It currently only works for the first two inputs (the ones with "<>" and partially for the others but I can't figure it out. I've been using regex101.com but still no luck.


Solution

  • To:\s*<?(sip:[^@]+@[^:]+:\d+)>?(?:;tag=([a-zA-Z]+))?
    
    • To: static prefix
    • \s* any amount of whitespaces
    • <? an optional < symbol
    • (sip:[^@]+@[^:]+:\d+) the first capture group
      • sip: the static sip protocol
      • [^@]+@[^:]+ the e-mail address
        • [^@]+ the part before the @ symbol
        • [^:]+ the part before the : symbol
      • :\d+ the port which consists of at least one digit
    • >? an optional > symbol
    • (?:;tag=([a-zA-Z]*))? the second (optional) capture group
      • ?:;tag= do not capture the static ;tag= prefix
      • ([a-zA-Z]+) capture the alphabet characters (there must be at least one character)

    regex 101