Python Regex: Matching A but not B

What will you learn?

By diving into this tutorial, you will grasp the essence of leveraging regular expressions in Python to precisely match patterns that involve including a specific substring A while excluding another substring B.

Introduction to the Problem and Solution

Imagine a scenario where you need to filter data or extract information from text with a defined structure. The challenge at hand is to identify instances where a particular substring A exists but without allowing the presence of another specific substring B. This is where the power of regular expressions (regex) in Python comes into play.

With regex patterns, we can set rules that encompass both the inclusion of substring A and the exclusion of substring B simultaneously. This level of control enables us to effectively extract or manipulate data based on our desired criteria. Whether it’s for data analysis, text processing, or any other task requiring pattern matching, mastering regex in Python opens up a world of possibilities.


import re

text = "Sample text containing A and possibly C but not B"
pattern = r'A(?!.*B).*'

matches = re.findall(pattern, text)
print(matches)  # Output: ['A and possibly C']

# Copyright PHD


In this code snippet, we utilize negative lookahead (?!…) within a regex pattern in Python: – r’A(?!.*B).*’: Defines the regex pattern to find ‘A’ (A) followed by any characters except for ‘B’ ((?!.*B)), zero or more times (.*). – re.findall(pattern, text): Applies the regex pattern on the input text string using re.findall() to discover all non-overlapping matches. – The result showcases instances where ‘A’ is present without being immediately followed by ‘B’.

This approach offers precise control over matching patterns with complex requirements involving multiple substrings.

    How does negative lookahead (?!…) work in regular expressions?

    Negative lookahead (?!…) ensures that a particular sequence does not directly follow the current position in the matched string.

    Can I use multiple negative lookaheads together?

    Yes, you can combine multiple negative lookaheads within a single regex expression for intricate matching conditions.

    Is it possible to have positive lookahead along with negative lookahead?

    Certainly! Positive and negative lookaheads can be combined within a regex pattern for comprehensive matching logic.

    Are there other similar techniques like negative lookaheads in regular expressions?

    Apart from negative lookaheads, you can explore positive lookaheads, as well as lookbehinds (both positive and negative), for advanced regex operations.

    Can I make my regex case-insensitive when applying such filters?

    Absolutely! You can enable case-insensitive matching by utilizing flags like re.IGNORECASE.

    Do I need prior knowledge of regular expressions before attempting such tasks?

    While basic understanding helps, practice and exploring online resources dedicated to learning regex concepts are beneficial for enhancing your skills gradually.


    Mastering the art of crafting intricate yet elegant regular expression patterns empowers developers and data analysts alike. By exploring features like negative lookahead assertions alongside fundamental constructs such as character classes or quantifiers; one unlocks endless possibilities within text processing workflows. Remember that practice makes perfect when it comes to leveraging potent tools like regEx libraries in programming languages like Python!

    Leave a Comment