Jump to content

Compare strings with wildcards and check if it match


Recommended Posts

Hello, Does anyone know an easy way to check string if its match with another string, that works with wildcards?

$URL = "http://something.something.com/something"
$BlockedURLS = "http://*.something.com/*|http://something2.com/ads*"
Edited by Chris86
Link to comment
Share on other sites

Ok, Im trying to find a way to check if a url match any of the blocked urls

All the blocked urls are in an array, but how can I compare them with wildcards etc?

$URL = "http://something.something.com/something"
$BlockedURLS = "http://*.something.com/*|http://something2.com/ads*"
$array = StringSplit($BlockedURLS, "|")
For $array = 1 to $array[0] Step 1
If 
;COMPARE $URL with $Array
    MsgBox(0,"", "URL match, cutting connection")
else
MsgBox(0,"", "No match, let it pass")
endif
Next
Edited by Chris86
Link to comment
Share on other sites

Here's an example that might help you get started :

Global $Blocked = 'something.com|google.com|autoitscript.com'

MsgBox (0, 'Test','Blocked : ' & _IsBlocked ('http://something.something.com/something'))
MsgBox (0, 'Test','Blocked : ' & _IsBlocked ('http://www.google.com/search?client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&channel=s&hl=en&source=hp&q=lol&btnG=Google+Search'))
MsgBox (0, 'Test','Blocked : ' & _IsBlocked ('http://www.autoitscript.com/forum/index.php?showtopic=112752'))
MsgBox (0, 'Test','Blocked : ' & _IsBlocked ('http://lol.com/'))

Func _IsBlocked ($URL)
    $Split = StringSplit ($Blocked, '|')
    For $A = 1 To $Split[0]
        If StringInStr ($URL, $Split[$A]) Then Return 1
    Next
EndFunc

Hope it helps! =]

- John

Edited by John2010zz

Latest Projects :- New & Improved TCP Chat

Link to comment
Share on other sites

Avoid bumping under 24h please.

Now to answer your question, you may succeed in doing what you want with regexps. The issue will be massaging your blocking patterns to fit RegExp grammar.

Try first replacing * by .* and . by \. in your patterns. Enclose them in perenthesis and concatenate them with |

You may need more replacements.

Quick untested try:

$URL = "http://something.something.com/something"
$BlockedURLS = "http://*.something.com/*|http://something2.com/ads*"
$BlockedURLS = StringReplace($BlockedURLS, ".", "\.")
$BlockedURLS = StringRegExpReplace($BlockedURLS, "(?<!\\)\*", "\.\*")
$array = StringSplit($BlockedURLS, "|")
$cut = False
For $i = 1 to $array[0]
    ;COMPARE $URL with $Array
    ConsoleWrite("comparing " & $url & " against pattern " & $array[$i] & @LF)
    $cut = $cut Or StringRegExp($URL, $array[$i])
Next
If $cut Then
    MsgBox(0,"", "URL match, cutting connection")
Else
    MsgBox(0,"", "Go ahead")
EndIf

You may as well concatenate as I said before. For instance:

$URL = "http://something.something.com/something"
$BlockedURLS = "http://*.something.com/*|http://something2.com/ads*"
$BlockedURLS = StringReplace($BlockedURLS, ".", "\.")
$BlockedURLS = StringRegExpReplace($BlockedURLS, "(?<!\\)\*", "\.\*")
$BlockedURLS = '(' & StringReplace($BlockedURLS, "|", ")|(") & ')'

ConsoleWrite("comparing " & $url & " against pattern " & $BlockedURLS & @LF)
$cut = StringRegExp($URL, $BlockedURLS)
If $cut Then
    MsgBox(0,"", "URL match, cutting connection")
Else
    MsgBox(0,"", "Go ahead")
EndIf

But keep in mind that doing so with hundreds of individual Urls all concatenated in an alternation pattern, then it will drag feet considerably!

Edit: also beware of referer Urls as you could block access to legitimate Urls that have blocked Urls as referers. How could users have got there previously is a good question.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...