TheCompModder TheCompModder - 3 months ago 9
Python Question

Regex - Match string with or without suffix, but without including it in match group

I'm currently trying to make a text message command server (basically, I send a text to google voice, that gets forwarded to my email, I use a python IMAP library to access the message, and I parse it), and I have an interesting problem. Sometimes, when a text comes through, the string

--
Sent using SMS-to-email. Reply to this email to text the sender back and save on SMS fees.
https://www.google.com/voice/


is appended to the text message, which causes errors when parsing the command. Right now, to detect commands, I use the following regex (which is multiline and case-sensitive due to certain commands such as a timed send command that may use multiple lines):

^/(randomfact) *(\S*)\s*$


But since the string telling me it was sent through SMS to email comes through once in a while, a match doesn't get detected. Using the
print
function of python, the message is shown as follows:

/randomfact\r\n\r\n--\r\nSent using SMS-to-email. Reply to this email to text the sender back and \r\nsave on SMS fees.\r\nhttps://www.google.com/voice/


Right now, to combat that issue, I have tried doing this:

^/(randomfact)\s*(\d*)(?=\n\n--\nSent using SMS-to-email\. Reply to this email to text the sender back and save on SMS fees\.\nhttps://www\.google\.com/voice/)


But it only works if the string IS appended to the command. If it isn't, then the regex fails. My question is: Is there any way to exclude that string from any regex matches, whether it exists in the string or not?

Answer
def remove_footer(incoming_str):
    footer = '''
--
Sent using SMS-to-email. Reply to this email to text the sender back and save on SMS fees.
https://www.google.com/voice/'''
    if incoming_str[-len(footer):] == footer:
        return incoming_str[:-len(footer)]
    else:
        return incoming_str

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Comments