Can I substitute regex as a string in python? To use in `find()`? [closed]

I am working in Python3 lately for Errbot, my XMPP and IRC bot. I want to make a plugin for it that will detect capital letters in any message string containing more than x amount of capitalized letters.

And this is the code I have now, halfarsed:

import re
from errbot import BotPlugin, botcmd

class Yell(BotPlugin):
    def callback_message(self, mess):
        mess = str(mess)
        caps = re.match('[A-Z]{1,}', mess)

        if mess.body.find(caps) >= 20: # occurs >= 20 times
            self.send(
                mess.frm.room,
                "AAAAAAAAAAAAAAAAAAAHHHHHHHhh!! ! !",
            )

EDIT: What I want is any string of any combination of characters, if it contains 20 or more capital letters in any place of the string, to return true. So if I can find 20 capital letters anywhere in any combination of characters it will execute the if statement. Otherwise ignores.

It’s a bot function that reacts to too many capital letters.

<re.Match object; span=(13, 16), match='FVF'> this as a print() output i cannot comprehend what to use it with or for?

Answer

You could use Python isupper and count the occurrence of uppercase letters.

text_sample = """
EDIT: What I want is any string of any combination of characters, if it contains 20 or more capital letters in any place of the string, to return true. So if I can find 20 capital letters anywhere in any combination of characters it will execute the if statement. Otherwise ignores.
It's a bot function that reacts to too many capital letters.
"""

alphabet = """ABCDEFGHIJKLMNOPQRSTUVWXYZ"""
result = len([i for i in text_sample if i.isupper()])
if result >= 20: # occurs >= 20 times
    print("AAAAAAAAAAAAAAAAAAAHHHHHHHhh!! ! !")
else:
    print("Fewer than 20 uppercase letters!")

#Fewer than 20 uppercase letters!
result = len([i for i in alphabet if i.isupper()])
if result >= 20: # occurs >= 20 times
    print("AAAAAAAAAAAAAAAAAAAHHHHHHHhh!! ! !")
else:
    print("Fewer than 20 uppercase letters!")

#AAAAAAAAAAAAAAAAAAAHHHHHHHhh!! ! !

Your code would turn to be something similar to:

from errbot import BotPlugin, botcmd

class Yell(BotPlugin):
    def callback_message(self, mess):
        mess = str(mess)
        upper_letters = len([i for i in mess if i.isupper()])

        if upper_letters >= 20: # occurs >= 20 times
            self.send(
                mess.frm.room,
                "AAAAAAAAAAAAAAAAAAAHHHHHHHhh!! ! !",
            )