Find all instances of a pattern in a text

Asked

Viewed 723 times

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]

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

python criptografia.py < entrada.txt

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

How can I solve?

  • Why did you put len(letra) - 7?

  • 1

    @Orion, because it checks from the current position. If you do not will exit the vector string.

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]

Exit:

the

the

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 running here

  • 1

    Thank you, man!!! I am a first term student, I started studying Python just over three months ago, I still don’t know all the functions. The answer was helpful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.