Find all instances of a pattern in a text

5

I need to write a program that identifies a lowercase letter surrounded by three uppercase letters on each side.

For example:

"AEIoSDE" == "o"

I wrote the code as follows:

# coding: utf-8
letra = raw_input()

for i in range(1, len(letra) - 7):
    if letra[i].isupper() and not letra[i-1].isupper():
     if letra[i+1].isupper():
      if letra[i+2].isupper():
       if letra[i+3].islower():
        if letra[i+4].isupper():
         if letra[i+5].isupper():
          if letra[i+6].isupper() and not letra[i+7].isupper():
            print letra[i+3]

I just need to do this for a massive amount of data, so I saved it to a .txt file and entered the Linux terminal and typed:

python criptografia.py < entrada.txt

But it returns only one letter and does not read the other strings.

How can I resolve?

    
asked by anonymous 15.08.2014 / 18:15

1 answer

4

Your code works normally here :

#!/usr/local/bin/python2.7
letra = "AEIoSDEaaAEIoSDEaaAEIoSDEAaaEIoSDEAEIoSDEAEIoSDEAEIoSDEAEIoSDEAEIoSDEAEIoSDEAEIoSDE"
for i in range(0, len(letra) - 7):
      if letra[i].isupper():
           if letra[i+1].isupper() and letra[i+2].isupper():
                 if letra[i+3].islower() and letra[i+4].isupper():
                      #print letra[i+3] + letra[i+4]
                      if letra[i+5].isupper():
                          if letra[i+6].isupper() and not letra[i+7].isupper():
                              print letra[i+3]

Output:

  

o

     

o

Note that you can also avoid all this amount of ifs with regex:

#!/usr/local/bin/python2.7

import re
padrao = r'[A-Z]{3}[a-z][A-Z]{3}[a-z]'
letra = "AEIoSDEaaAEIoSDEaaAEIoSDEAaaEIoSDEAEIoSDEAEIoSDEAEIoSDEAEIoSDEAEIoSDEAEIoSDEAEIoSDE"
for enc in re.findall(padrao , letra):
    print enc[3]

You can see this example by running here

    
15.08.2014 / 18:56