Jump to content

Regex filter - If/Else


Recommended Posts

Hello,

there is a filter where I can run script only if query matches reges.

For example I can do:

^(ERROR:)

 

So script will continue on only if query starts with "ERROR:".

But how can I do something like "else" fir this "if"?

I found out I can do second filter and put there:

^(?!ERROR:)
So another regex that means "does not start with ERROR:".

 

But what if there is really difficult regex? Do I really need to rewrite it?
Or is there some way to do ELSE for filter?

Link to comment
1 hour ago, Meldiron said:

Or is there some way to do ELSE for filter?

 

If you're trying to implement any but the most trivial logic, you're better off doing it in code. In this case, use a Run Script with something like:

# this is Python
import re
import sys
if re.match(r'\(ERROR:\).+', sys.argv[1]):
    print "yes"
else:
    print "no"


And then have your filters match {query} = yes or {query} = no (or just leave one of them empty).
 

Edited by deanishe
Link to comment
  • 4 weeks later...

@deanishe - thanks for sharing that. I am trying to do something similar, but I want it to look for multiple terms.

 

My query: http://abc.go.com/

 

My (python) Run Script, taken from your suggestion, but using re.search instead of re.match:
 

# this is Python
import re
import sys
if re.search(r"abc.go", sys.argv[1]):
    
	print "yes"
else:
    print "no"

 

My question is, how can I edit the script to also see if it matches other portions of URLs? For instance, cbs.com - how could I edit the script to also check for that?

 

My apologies for my lack of familiarity with python and regex both. I really appreciate all of the help and examples you provide in these forums! 

Link to comment
17 minutes ago, joeynotjoe said:

@deanishe - thanks for sharing that. I am trying to do something similar, but I want it to look for multiple terms.

 

My query: http://abc.go.com/

 

My (python) Run Script, taken from your suggestion, but using re.search instead of re.match:
 


# this is Python
import re
import sys
if re.search(r"abc.go", sys.argv[1]):
    
	print "yes"
else:
    print "no"

 

My question is, how can I edit the script to also see if it matches other portions of URLs? For instance, cbs.com - how could I edit the script to also check for that?

 

My apologies for my lack of familiarity with python and regex both. I really appreciate all of the help and examples you provide in these forums! 

 

Add another clause:

# this is Python
import re
import sys
query = sys.argv[1]
if re.search(r"abc.go", query):
	print "yes"
elif re.search(r'cbs\.com', query):
    print "yes"
else:
    print "no"

Better yet, don't use regular expressions. They're difficult and rarely the right solution. (For example, your abc.go pattern is incorrect. In a regular expression, . means "any character". It should be abc\.go):

import sys
from urlparse import urlparse

domains = ['abc.go.com', 'cbs.com', 'google.com']
query = sys.argv[1]

u = urlparse(query)

if u.hostname in domains:
    print "yes"
else:
    print "no"

Or just check for substrings:

import sys

wanted = ['abc.go', 'cbs.com', 'google.com']
query = sys.argv[1]

for s in wanted:
    if s in query:
        print "yes"
        break
else:
    print "no"
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...