Regular expression negative look-ahead

J

Jason Friedman

I have table names in this form:
MY_TABLE
MY_TABLE_CTL
MY_TABLE_DEL
MY_TABLE_RUN
YOUR_TABLE
YOUR_TABLE_CTL
YOUR_TABLE_DEL
YOUR_TABLE_RUN

I am trying to create a regular expression that will return true for only
these tables:
MY_TABLE
YOUR_TABLE

I tried these:
pattern = re.compile(r"_(?!(CTL|DEL|RUN))")
pattern = re.compile(r"\w+(?!(CTL|DEL|RUN))")
pattern = re.compile(r"(?!(CTL|DEL|RUN)$)")

But, both match.
I do not need to capture anything.
 
N

Neil Cerutti

I have table names in this form:
MY_TABLE
MY_TABLE_CTL
MY_TABLE_DEL
MY_TABLE_RUN
YOUR_TABLE
YOUR_TABLE_CTL
YOUR_TABLE_DEL
YOUR_TABLE_RUN

I am trying to create a regular expression that will return true for only
these tables:
MY_TABLE
YOUR_TABLE

Use the "is not a word" character class on either end.

r"\WMY_TABLE\W"
r"\WYOUR_TABLE\W"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top