title: How to match start or end of given string using regex in java
body: <p>I want to match a string which starts or ends with a "test" using regex in java.</p>
  
  <p>Match Start of the string:-
        String a = "testsample";</p>
  
  <pre><code>  String pattern = "^test";
  
    // Create a Pattern object
    Pattern r = Pattern.compile(pattern);
  
    // Now create matcher object.
    Matcher m = r.matcher(a);
  </code></pre>
  
  <p>Match End of the string:-
        String a = "sampletest";</p>
  
  <pre><code>  String pattern = "test$";
  
    // Create a Pattern object
    Pattern r = Pattern.compile(pattern);
  
    // Now create matcher object.
    Matcher m = r.matcher(a);
  </code></pre>
  
  <p>How can I combine regex for starts or ends with a given string ?</p>