0

I've written a regex so when a user writes in the chat "Kosovo", my bot will send him an audio and a text.

    regex = "k+o+s+o+v+o"
try:
            nick = update.message.from_user.username
            if re.search(regex, contLower):
                    chatid = update.message.chat.id
                    audio1 = "/Users/rikardo/Documents/ANGELO/Programmare/Prove di basi/Mat/SUONI/KosovoBello.ogg"
                    update.message.reply_audio(audio=open(audio1, "rb"))
                    update.message.reply_text("Stay tuned, @PadriFibra")
                    return
except Exception as err:
    print(err)

return

Then I added this:

print(strftime("%H:%M:%S -")," "+(nick)+" triggered KosovoBello")

and the result was this:

    regex = "k+o+s+o+v+o"
try:
            nick = update.message.from_user.username
            if re.search(regex, contLower):
                    chatid = update.message.chat.id
                    audio1 = "/Users/rikardo/Documents/ANGELO/Programmare/Prove di basi/Mat/SUONI/KosovoBello.ogg"
                    update.message.reply_audio(audio=open(audio1, "rb"))
                    update.message.reply_text("Stay tuned, @PadriFibra")
                    print(strftime("%H:%M:%S -")," "+(nick)+" triggered KosovoBello")
                    return
except Exception as err:
    print(err)

return

But when I try to start the bot, the console says this:

    $ python3 /Users/rikardo/Documents/ANGELO/Programmare/Prove\ di\ basi/Mat/prova.py 
  File "/Users/rikardo/Documents/ANGELO/Programmare/Prove di basi/Mat/prova.py", line 107
    regex = "k+o+s+o+v+o"
        ^
SyntaxError: invalid syntax
user94559
  • 59,196
  • 6
  • 103
  • 103
  • It would be good if you could share the code exactly as it is, since indentation matters in python. – Bonifacio2 Jun 07 '17 at 16:06
  • 2
    Could you include some of the code above this line too? Also, please fix the indentation and make sure it looks exactly like your actual code. (E.g., is the first line of the code you shared truly indented more than the line after it?) – user94559 Jun 07 '17 at 16:06
  • Also, you seem to think it was the addition of the `print` line that caused the error, but that certainly doesn't seem to be the case. – user94559 Jun 07 '17 at 16:08
  • 1
    you need to fix your indents - e.g. first line should not be indented. Also, what are your imports/methods? Is regex already a method? – owns Jun 07 '17 at 16:12
  • I solved my problem. I don't even remember how, lmao. I'm sorry if I didn't explain better the problem, it was my first question on stackflow. – Lord Pigeon Sep 18 '17 at 21:03

1 Answers1

-1

This is not exactly correct. By specifying '+' after each character you allow matching of things like kkooosoovvvvvvo :-)

Instead, you should just use the exact string you want:

regex="(kosovo)"

I'd also suggest including case variations

regex="(kosovo|Kosovo|KOSOVO)"

or just using IGNORECASE so that every variation is covered.