title: Simplify java regex code to validate passwords
body: <p>I want to inform the user what he needs so that his password is valid, when it does not meet the requirements the <code>jlabels</code> changes color RED and when it meets it changes to Black. The shortest way that I could do what I want, but it will exist other? I attach the code and a photo</p>
  <pre><code>    private void JB_MostrarActionPerformed(java.awt.event.ActionEvent evt) {                                           
  
      String password = &quot;&quot;;
  
      password = jPasswordField_pass.getText().trim();
  
      final String regex = &quot;^(?:(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&amp;+=.,]).*)[^\\s]{8,}$&quot;;
  
      if (password.equals(&quot;&quot;)) {
          jl_Error.setText(&quot;Please enter your Password.&quot;);
          jl_Error.setForeground(Color.red);
  
      } else if (password.matches(regex) == false) {
  
          jLabel_infopass.setText(&quot;Your password must be between 8 and 64 characters long. It must contain at least three (3) of the below:&quot;);
          jLabel_infopass.setForeground(Color.red);
  
          if (Pattern.matches(&quot;(?=.*\\d).*&quot;, password) == false) {
              jLabel_infopass3.setText(&quot;numbers (0-9)&quot;);
              jLabel_infopass3.setForeground(Color.red);
          } else {
              jLabel_infopass3.setText(&quot;numbers (0-9)&quot;);
              jLabel_infopass3.setForeground(Color.black);
          }
          if (Pattern.matches(&quot;(?=.*[a-z]).*&quot;, password) == false) {
              jLabel_infopass2.setText(&quot;lowercase (a-z)&quot;);
              jLabel_infopass2.setForeground(Color.red);
          } else {
              jLabel_infopass2.setText(&quot;lowercase (a-z)&quot;);
              jLabel_infopass2.setForeground(Color.black);
          }
          if (Pattern.matches(&quot;(?=.*[A-Z]).*&quot;, password) == false) {
              jLabel_infopass1.setText(&quot;uppercase (A-Z)&quot;);
              jLabel_infopass1.setForeground(Color.red);
          } else {
              jLabel_infopass1.setText(&quot;uppercase (A-Z)&quot;);
              jLabel_infopass1.setForeground(Color.black);
          }
          if (Pattern.matches(&quot;(?=.*[@#$%^&amp;+!.,]).*&quot;, password) == false) {
              jLabel_infopass4.setText(&quot;symbols (e.g. #, $, !, @, ^, &amp;, *, etc)&quot;);
              jLabel_infopass4.setForeground(Color.red);
          } else {
              jLabel_infopass4.setText(&quot;symbols (e.g. #, $, !, @, ^, &amp;, *, etc)&quot;);
              jLabel_infopass4.setForeground(Color.black);
          }
  
      } else {
          jLabel_infopass.setText(&quot;Your password must be between 8 and 64 characters long. It must contain at least three (3) of the below:&quot;);
          jLabel_infopass.setForeground(Color.black);
          JOptionPane.showMessageDialog(null, password);
      }
  } 
  </code></pre>
  <p><a href="https://i.stack.imgur.com/p9OdE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p9OdE.png" alt="capture run box" /></a></p>