-1

I'd like to write a function that matches in a string and returns all uppercase letters that are not followed by a lowercase letter.

def find_sub_string(text, pattern):
        if re.search(pattern,  text):
            return re.findall(pattern,  text)
        else:
            return None

Here is an example of a desired output:

>>> find_sub_string('AaAcDvFF(G)F.E)f',  pattern)
>>> ['F', 'F', 'G', 'F', 'E']

What should be the right pattern in that case?

I tried pattern='[A-Z][^a-z], but it produce the following output ['FF', 'G)', 'F.', 'E)'], which satisfies the described condition, except it returns two letters except one.

user48115
  • 445
  • 1
  • 8
  • 18
  • Thank you, mine question was totally similar to the previously asked question. In my case the answer I was looking for was ```pattern='[A-Z](?![a-z])'`` – user48115 Nov 22 '19 at 09:42

1 Answers1

0

USe negative look ahead in rgex

import re
def find_sub_string(text, pattern):
        if re.search(pattern,  text):
            return re.findall(pattern,  text)
        else:
            return None
find_sub_string('AaAcDvFF(G)F.E)f',  '[A-Z](?![a-z])')
ArunJose
  • 1,999
  • 1
  • 10
  • 33