<div dir="ltr">Huh, did not realize that endswith takes a list. I'll remember that in the future.<div><br></div><div>This need is actually for <a href="http://schemaspy.sourceforge.net/">http://schemaspy.sourceforge.net/</a>, which allows one to include only tables/views that match a pattern.</div>
<div><br></div><div>Either there is a bug in Schemaspy's code or Java's implementation of regular expressions is different than Python's or there is a flaw in my logic, because the pattern I verify using Python produces different results when used with Schemaspy. I suppose I'll open a bug there unless I can find the aforementioned flaw.</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jul 1, 2013 at 11:44 PM, Ian Kelly <span dir="ltr"><<a href="mailto:ian.g.kelly@gmail.com" target="_blank">ian.g.kelly@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Mon, Jul 1, 2013 at 8:27 PM, Jason Friedman <<a href="mailto:jsf80238@gmail.com">jsf80238@gmail.com</a>> wrote:<br>
> Found this:<br>
> <a href="http://stackoverflow.com/questions/13871833/negative-lookahead-assertion-not-working-in-python" target="_blank">http://stackoverflow.com/questions/13871833/negative-lookahead-assertion-not-working-in-python</a>.<br>
><br>
> This pattern seems to work:<br>
> pattern = re.compile(r"^(?!.*(CTL|DEL|RUN))")<br>
><br>
> But I am not sure why.<br>
><br>
><br>
> On Mon, Jul 1, 2013 at 5:07 PM, Jason Friedman <<a href="mailto:jsf80238@gmail.com">jsf80238@gmail.com</a>> wrote:<br>
>><br>
>> I have table names in this form:<br>
>> MY_TABLE<br>
>> MY_TABLE_CTL<br>
>> MY_TABLE_DEL<br>
>> MY_TABLE_RUN<br>
>> YOUR_TABLE<br>
>> YOUR_TABLE_CTL<br>
>> YOUR_TABLE_DEL<br>
>> YOUR_TABLE_RUN<br>
>><br>
>> I am trying to create a regular expression that will return true for only<br>
>> these tables:<br>
>> MY_TABLE<br>
>> YOUR_TABLE<br>
>><br>
>> I tried these:<br>
>> pattern = re.compile(r"_(?!(CTL|DEL|RUN))")<br>
>> pattern = re.compile(r"\w+(?!(CTL|DEL|RUN))")<br>
>> pattern = re.compile(r"(?!(CTL|DEL|RUN)$)")<br>
>><br>
>> But, both match.<br>
>> I do not need to capture anything.<br>
<br>
<br>
</div></div>For some reason I don't seem to have a copy of your initial post.<br>
<br>
The reason that regex works is because you're anchoring it at the<br>
start of the string and then telling it to match only if<br>
".*(CTL|DEL|RUN)" /doesn't/ match. That pattern does match starting<br>
from the beginning of the string, so the pattern as a whole does not<br>
match.<br>
<br>
The reason that the other three do not work is because the forward<br>
assertions are not properly anchored. The first one can match the<br>
first underscore in "MY_TABLE_CTL" instead of the second, and then the<br>
next three characters are "TAB", not any of the verboten strings, so<br>
it matches. The second one matches any substring of "MY_TABLE_CTL"<br>
that isn't followed by "CTL". So it will just match the entire string<br>
"MY_TABLE_CTL", and the rest of the string is then empty, so does not<br>
match any of those three strings, so it too gets accepted. The third<br>
one simply matches an empty string that isn't followed by one of those<br>
three, so it will just match at the very start of the string and see<br>
that the next three characters meet the forward assertion.<br>
<br>
Now, all that said, are you sure you actually need a regular<br>
expression for this? It seems to me that you're overcomplicating<br>
things. Since you don't need to capture anything, your need can be<br>
met more simply with:<br>
<br>
if not table_name.endswith(('_CTL', '_DEL', '_RUN')):<br>
# Do whatever<br>
<span class="HOEnZb"><font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br></div>