How to check validity of cookies in java using regex
<p>I have a question regarding validation of cookies in java. I went through  [RFC standards for internet cookie][1]
[1]: https://www.rfc-editor.org/rfc/rfc6265#section-4.1.1
tutorial and looked at the predefined correct syntax. Now I have a java code with some cookie examples and I would like to apply regular expression that tell if the given cookies are valid or not. Below is my code and the regex I applied:</p>
<p><strong>Code Snippet :</strong></p>
<pre><code>    import java.util.regex.Matcher;
    import java.util.regex.Pattern;



    public class CheckValidity

     {
        public static boolean IsValid(String mycookie)
    
      {
        boolean valid = false;
        String pattern=&quot;^Set-Cookie:\\s\\*?\\=\\*?\\;\\^Expires=\\^Path=\\^Domain=.*\\^Max-Age=[1-9]\\d*$\\HttpOnly&quot;;
        Pattern p= Pattern.compile(pattern);
        Matcher m=p.matcher(mycookie);
       
        
        for (int k=0; k&lt;mycookie.length();k++)
        
        {  
         
            if(m.find())
            {
                valid=true;
                return valid;
            }
          
            else
            {  
                valid=false;
                return valid;
            }    
        }
               return valid;
        }  
        
    
    public static void main(String[] args) {
        
            String [] examples = {
           
                                              // valid examples: 1 to 7
            &quot;Set-Cookie: ns5=&quot;,                                                             // 1 valid: value is empty
            &quot;Set-Cookie: ns5=\&quot;alss/0.asdf^\&quot;&quot;,                                             // 2 valid: name=value
            &quot;Set-Cookie: ns5=\&quot;alss/0.asdf^\&quot;; Expires=Tue, 15 Jan 2006 14:24:53 GMT&quot;,      // 3 valid: Expires=arbitrary time
            &quot;Set-Cookie: ns5=; Domain=&quot;,                                                    // 4 valid: empty domain
            &quot;Set-Cookie: ns5=; Domain=.srv.a.com-0&quot;,                                        // 5 valid: Domain=host_name
            &quot;Set-Cookie: ab=Asdfa; Expires=Sat, 15 Jan 2006 14:24:53 GMT; Path=/; Domain=.example.com; HttpOnly&quot;, //6 Valid
            &quot;Set-Cookie: ns5=alss/0.asdf^; HttpOnly&quot;,                                       // 7 valid
            &quot;Set-Cookie: ns5=alss/0.asdf^; httponly&quot;,                   // 8  invalid H and O should be capital
            &quot;Set-Cookie:&quot;,                                              // 9  invalid empty 
            &quot;Set-Cookie: bv&quot;,                                           // 10 invalid cookie-pair: missing '=' 
            &quot;Set-Cookie: =alss/0.asdf^&quot;,                                // 11 invalid cookie-pair: name is empty
            &quot;Set-Cookie: ns@5=alss/0.asdf^&quot;,                            // 12 invalid cookie-pair: name is invalid
            &quot;Set-Cookie: ns5=alss/0.asdf^;&quot;,                            // 13 invalid redundant ';' after ^
            &quot;Set-Cookie: ns5=; Expires=Tue 15 Jan 2006 14:24:53 GMT&quot;,   // 14 invalid Expires missing ',' after Tue
            &quot;Set-Cookie: ns5=alss/0.asdf^; Max-Age=06&quot;,                 // 15 invalid Max-Age: starting 0
            &quot;Set-Cookie: ns5=alss/0.asdf^; Domain=.0com&quot;,               // 16 invalid Domain: starting 0
            &quot;Set-Cookie: ns5=alss/0.asdf^; Domain=.com-&quot;,               // 17 invalid Domain: trails non-letter-digit
            &quot;Set-Cookie: ns5=alss/0.asdf^; Path=&quot;,                      // 18 invalid Path: empty
            
        };
        
       for (int i = 0; i &lt; examples.length; i++)
           
        System.out.println(String.format(&quot;Cookie %d: %s&quot;, i+1, IsValid(examples[i]) ? &quot; is valid&quot; : &quot;is invalid&quot;));
    }

}




  

 

    
</code></pre>
<p>To be honest, I did not work on regular expressions before, so I read some tutorials and then applied my current regex. I think there are some cases that I miss in my regex, especially <code>AND</code> and <code>OR</code> operators but I could not really find the correct syntax for this. If possible, could you tell me how should the regex be so that it complies with the cookie standards and number one to seven retrieve &quot;valid&quot; and remaining ones &quot;invalid&quot;. I really appreciate your help. Thanks in advance!</p>