Question: I know that there are different ways to do this, but I just want to know why my regex isn't working. This isn't actually something that I need to do, I just wanted to see if I could do this with a regex, and I have no idea why my code isn't working.

Given a string S, I want to find all non-overlapping substrings that contain a subsequence Q that obeys certain rules. Now, let's suppose that I am searching for the subsequence `"abc"`. I want to match a substring of S that contains `'a'` followed at some point by `'b'` followed at some point by `'c'` with the restriction that no `'a'` follows `'a'`, and no `'a'` or `'b'` follows `'b'`. The regex I am using is as follows (in python):

```
regex = re.compile(r'a[^a]*?b[^ab]*?c')
match = re.finditer(regex, string)

for m in match:
    print m.group(0)

```

To me this breaks down and reads as follows:

`a[^a]*?b`: `'a'` followed the smallest # of characters not including `'a'` and terminating with a `'b'`   

`[^ab]*?c`: the smallest # of characters not including `'a'` or `'b'` and terminating with a `'c'`

So putting this all together, I assumed that I would match non-overlapping substrings of S that contain the subsequence "abc" that obeys my rules of exclusion. 

This **works fine** for something like:

`S = "aqwertybwertcaabcc"`, which gives me `"aqwertybwertc"` and `"abc"`,

but it **fails** to work for `S = "abbc"`, as in it matches to `"abbc"`.

Answer: 
Assuming what you actually want is for the subsequence Q to contain no `a`s between the first `a` and the first `b` and no `a`s or `b`s between the first `b` and the first `c` after the first `b`, the correct regex to use is:

```
r'a[^ab]*b[^abc]*c'

```

The regex that you're currently using will do everything that it can to succeed on a string, including matching the literal `b` to a `b` after the first `b`, which is why `"abbc"` is matched. Only by specifically excluding `b` in the first character class can this be avoided and the `b` be made to match only the first `b` after the `a`.
It could help if you look at the inverse class.  

In all cases `abc` is the trivial solution.  

And, in this case non-greedy probably doesn't apply because  

there are fixed sets of characters used in the example inverse classes. 

```
 #  Type 1 :
 #  ( b or c can be between A,B )
 #  ( a or b can be between B,C )
 # ------------------------------
 a                    #  'a'              
 [b-z]*?              #  [^a]
 b                    #  'b'
 [abd-z]*?            #  [^c]
 c                    #  'c'

 #  Type 2, yours :
 #  ( b or c can be between A,B )
 #  ( c can be between B,C )
 # ------------------------------
 a                    #  'a'              
 [b-z]*?              #  [^a]
 b                    #  'b'
 [c-z]*?              #  [^ab]
 c                    #  'c'

 #  Type 3 :
 #  ( c can be between A,B )
 # ------------------------------
 a                    #  'a'              
 [c-z]*?              #  [^ab]
 b                    #  'b'
 [d-z]*?              #  [^abc]
 c                    #  'c'

 #  Type 4 :
 #  ( distinct A,B,C ) :
 # ------------------------------
 a                    #  'a'              
 [d-z]*?              #  [^abc]
 b                    #  'b'
 [d-z]*?              #  [^abc]
 c                    #  'c'

```