<p>I am looking for a regex that will match a string that starts with one substring and does not end with a certain substring.</p>

<p>Example:</p>

<pre><code>// Updated to be correct, thanks @Apocalisp
^foo.*(?&lt;!bar)$
</code></pre>

<p>Should match anything that starts with "foo" and doesn't end with "bar".  I know about the [^...] syntax, but I can't find anything that will do that for a string instead of single characters.  </p>

<p>I am specifically trying to do this for Java's regex, but I've run into this before so answers for other regex engines would be great too.  </p>

<p>Thanks to @Kibbee for verifying that this works in C# as well.  </p>

[{'answer_id': 43010, 'author': 'Sam Hasler', 'author_id': 2541, 'author_profile': 'https://Stackoverflow.com/users/2541', 'pm_score': 1, 'selected': False, 'text': '<p>I\'m not familiar with Java regex but documentation for the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html#special" rel="nofollow noreferrer">Pattern Class</a> would suggest you could use (?!X) for a non-capturing zero-width negative lookahead (it looks for something that is not X at that postision, without capturing it as a backreference). So you could do:</p>\n\n<pre><code>foo.*(?!bar) // not correct\n</code></pre>\n\n<p><strong>Update</strong>: Apocalisp\'s right, you want negative lookbehind. (you\'re checking that what the .* matches doesn\'t end with bar)</p>\n'}, {'answer_id': 43016, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 0, 'selected': False, 'text': '<p>As other commenters said, you need a negative lookahead. In Java you can use this pattern:</p>\n\n<pre><code>"^first_string(?!.?second_string)\\\\z"\n</code></pre>\n\n<ul>\n<li>^ - ensures that string starts with\nfirst_string</li>\n<li>\\z - ensures that string ends with second_string</li>\n<li>(?!.?second_string) - means that first_string can\'t be followed by second_string</li>\n</ul>\n'}, {'answer_id': 43017, 'author': 'Apocalisp', 'author_id': 3434, 'author_profile': 'https://Stackoverflow.com/users/3434', 'pm_score': 4, 'selected': True, 'text': '<p>I think in this case you want <strong>negative lookbehind</strong>, like so:</p>\n\n<pre><code>foo.*(?&lt;!bar)\n</code></pre>\n'}, {'answer_id': 43030, 'author': 'John Meagher', 'author_id': 3535, 'author_profile': 'https://Stackoverflow.com/users/3535', 'pm_score': 1, 'selected': False, 'text': '<p>Verified @Apocalisp\'s answer using:</p>\n\n<pre><code>import java.util.regex.Pattern;\npublic class Test {\n  public static void main(String[] args) {\n    Pattern p = Pattern.compile("^foo.*(?&lt;!bar)$");\n    System.out.println(p.matcher("foobar").matches());\n    System.out.println(p.matcher("fooBLAHbar").matches());\n    System.out.println(p.matcher("1foo").matches());\n    System.out.println(p.matcher("fooBLAH-ar").matches());\n    System.out.println(p.matcher("foo").matches());\n    System.out.println(p.matcher("foobaz").matches());\n  }\n}\n</code></pre>\n\n<p>This output the the right answers:</p>\n\n<pre><code>false\nfalse\nfalse\ntrue\ntrue\ntrue\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/42990', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3535/']