title: Java Regex: find single quoted text with escapes
body: <p>This method should return all occurrences of single-quoted strings. However, an escaped single quote (<code>\’</code>) should be treated as a regular single-quote character (just like an escaped double-quote in Java). Example:  “<code>This ’isn\’t’ easy’</code>” the method should return the single string <code>“isn’t</code>”.<br>
  My code:</p>
  
  <pre><code>public static List&lt;String&gt; findSingleQuotedTextWithEscapes(String input) {
          Pattern pattern = Pattern.compile ("(?:\\w|'[^']*')+");
          Matcher matcher = pattern.matcher (input);
          ArrayList ans = new ArrayList();
          while (matcher.find ()){
              ans.add (matcher.group ().replace ("'",""));
          }
          return ans;
      }
  </code></pre>
  
  <p>Input: <code>"more'test'"</code> Expected: <code>[test]</code> Actual: <code>[more,test]</code><br>
  I seem to have a problem with catching just the ' character and I'm tokenizing everything, please help.
  Adding tester:</p>
  
  <pre><code>void fillSingleQuotedTestInputs(List&lt;String&gt; inputs, List&lt;List&lt;String&gt;&gt; expect) {
          inputs.add("'test'"); expect.add(Arrays.asList("test"));
          inputs.add("more'test'"); expect.add(Arrays.asList("test"));
          inputs.add("'test'more"); expect.add(Arrays.asList("test"));
          inputs.add("\\'no'yes'"); expect.add(Arrays.asList("yes"));
          inputs.add("a 'one' and 'two' and 'three'..."); expect.add(Arrays.asList("one", "two", "three"));
          inputs.add("nothing at all"); expect.add(Arrays.&lt;String&gt;asList());
          inputs.add("''"); expect.add(Arrays.asList(""));
          inputs.add("''test"); expect.add(Arrays.asList(""));
          inputs.add("test''"); expect.add(Arrays.asList(""));
          inputs.add("te''st"); expect.add(Arrays.asList(""));
          inputs.add("'This is not wrong' and 'this isn\\'t either'"); expect.add(Arrays.asList("This is not wrong", "this isn't either"));
          inputs.add("'tw\\'o repl\\'acements' in 't\\'wo stri\\'ngs'."); expect.add(Arrays.asList("tw'o repl'acements", "t'wo stri'ngs"));
          inputs.add("'\\''"); expect.add(Arrays.asList("'"));
          inputs.add("'''"); expect.add(Arrays.asList(""));
          inputs.add("'test1'\n'test2'"); expect.add(Arrays.asList("test1", "test2"));
          inputs.add("''''"); expect.add(Arrays.asList("", "")); // This one is hard. Hint: \G
      }
  
      @Test
      public void testFindSingleQuotedTextWithEscapes() {
          ArrayList&lt;String&gt; inputs = new ArrayList&lt;String&gt;();
          ArrayList&lt;List&lt;String&gt;&gt; expect = new ArrayList&lt;List&lt;String&gt;&gt;();
  
          fillSingleQuotedTestInputs(inputs, expect);
  
          for (int i = 0; i &lt; inputs.size(); ++i) {
              List&lt;String&gt; output = RegexpPractice.findSingleQuotedTextWithEscapes(inputs.get(i));
              assertEquals(String.format("Test %d failed: Search &lt;&lt;%s&gt;&gt;", i, inputs.get(i)), expect.get(i), output);
          }
      }
  </code></pre>