title: Regex in Spring Boot YML
body: <p>I have a Spring Boot web app:</p>
  
  <p>In application.properties, I have this property whose value is a regex:</p>
  
  <pre><code>mytest.regex=^[a-zA-Z0-9()\\-=\\*\\.\\?\\|;,+\\/:&amp;_ \"]*$
  </code></pre>
  
  <p>I inject that in a controller as below:</p>
  
  <pre><code>@Value("${mytest.regex}")
  private String regex;
  </code></pre>
  
  <p>and print in the controller method and then compile using:</p>
  
  <pre><code>Pattern.compile(regex)
  </code></pre>
  
  <p>It compiles successfully without any exception and prints the below line in the console (Note the Java related escape characters are not is the output):</p>
  
  <pre><code>^[a-zA-Z0-9()\-=\*\.\?\|;,+\/:&amp;_ "]*$
  </code></pre>
  
  <p>Now, I replace the application.properties file with application.yml. The file content now looks like this:</p>
  
  <pre><code>mytest:
    regex: ^[a-zA-Z0-9()\\-=\\*\\.\\?\\|;,+\\/:&amp;_ \"]*$
  </code></pre>
  
  <p>Please note that the regex are exact same.</p>
  
  <p>The console output now is:</p>
  
  <pre><code>^[a-zA-Z0-9()\\-=\\*\\.\\?\\|;,+\\/:&amp;_ \"]*$
  </code></pre>
  
  <p>and the <code>Pattern.compile(regex)</code> throws <code>java.util.regex.PatternSyntaxException: Illegal character range near index 16</code></p>
  
  <p>Now, I escape the <code>=</code> in the regex as:</p>
  
  <pre><code>^[a-zA-Z0-9()\\-\\=\\*\\.\\?\\|;,+\\/:&amp;_ \"]*$
  </code></pre>
  
  <p>The console output is (Note unlike properties file Java related escape characters are still there):</p>
  
  <pre><code>^[a-zA-Z0-9()\\-\\=\\*\\.\\?\\|;,+\\/:&amp;_ \"]*$
  </code></pre>
  
  <p>and <code>Pattern.compile(regex)</code> is successful.</p>
  
  <p>Now, please can someone explain:</p>
  
  <ol>
  <li>Why in case of yml, the console output shows Java related escape characters</li>
  <li>Why <code>=</code> needs to be escaped in yml, but not in properties</li>
  </ol>