RegEx help to match a word but exclude if it occurs in an URL


#1

Hello everyone,

first of all: thank you for your amazing bot!

At the moment I got a little problem with the keyword feature using RegEx. I want to setup a few keywords to trigger a response, but only if the keyword does not occur in an URL.

Example
Keyword: youtube

Response should be triggered if a user writes:
Do you have a youtube channel?
Are you on YouTube?
Is there a YOUTUBE channel?

and so on. It should trigger with any possible way a user could write youtube, except if it’s inside of an URL, like in song requests or links that are postet.

I did not figure it out myself yet, so I thought your support might be useful and also others will benefit from it.

Thank you and kind regards
kezpa


#2

\byoutube\b


#3

I already tried that. Unfortunately it will still get triggered when I post a youtube link


#4

Is link filter enabled? If yes, add youtube.com & youtu.be to whitelist.


#5

Link filter is disabled.


#6

here is one example I tried, but it somehow does not work (even though it does on regex101.com):

(?!youtube.com|youtu.be)\b(youtube|yt)\b


#7

Try this:
(?=(?![\.\/])youtube(?!\.com))


#8

Thanks, it works like that but I would like to have everything in a single expression. Like that I would need to make multiple keywords with expressions for YOUTUBE, youtu.be, YouTube etc.

Any chance you can help me with that?


#9

YOUTUBE, YouTube and even yOuTuBe are already catching by this regex.
If you don’t use link filter then there is no need to take into account youtu.be, I think.


#10

Well, that is exactly the problem: they are not.

I expect the bot to reply on the keywords YOUTUBE, YouTube and even yOuTuBe - but right now the bot only replied when I enter youtube or a sentence like “can I find you on youtube?”


#11

I just tried and everything works for me. What version of PhantomBot do you have?


#12

PhantomBot Version: 2.4.2 (Revision: 5c75a7b2)
Operating System: Linux
Java Version: 1.8.0_131-8u131-b11-1~bpo8+1-b11

I should also mention that I am using the beta-panel:
Panel Version: 1.2 (BETA)
Panel Template: AdminLTE


#13

Me too using beta-panel. I have no idea why caps are not caught by regex in your chat. The only thing I can advise is to reboot the bot.


#14

I just did. Didn’t help :frowning:


#15

Here’s how it happens in my console:

userName: youtube
[CHAT] .timeout userName 5 Blacklisted word
userName: YouTube
[CHAT] .timeout userName 5 Blacklisted word

Therefore, the question goes to another problem: why your bot’s regex is a case sensitive.


#16

So you are using the blacklist feature?
Just to be clear: I am using the keywords feature, I don’t see a need for the blacklisting in this case


#17

Oh, I’m sorry for my inattention :smiley:

For some reason, the bot does not perceive /.../i which allows you to ignore case.

I know this is the stupidest regex ever but it should work:
(?=(?![\.\/])[Yy][Oo][Uu][Tt][Uu][Bb][Ee](?!\.[Cc][Oo][Mm]))

NOTE: Don’t add this through the beta-panel, it turns uppercase letters into lowercase, add it through the old panel.


#18

No problem, Sir!
I am the grateful one getting help by you.

This finally worked - but did we just discover a bug in the RegEx for the keywords feature? @devs? :smiley:


#19

We implement a case-sensitive regular expression check. The RegExp() constructor doesn’t accept flags within the pattern. There is an optional parameter for flags on the constructor that we do not expose.


#20

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.