Jump to content

Regular expression workout ? :(


drislaam
 Share

Recommended Posts

Try this...

;## RegEx Web Example
#Include <Array.au3>

Dim Const $PermitedDomains = "zmelody|foxmoviez"
Dim $TestString = "I think example.com is a great site! But then again www.foxmoviez.com " & _
        "is the Shizzle! Not to be confused with that evil site at www.foxmoviez.au. To bad work " & _
        "blocks my access to zmelody at http://www.zmelody.com"

_URL($TestString, $PermitedDomains)

$Results = _URL($TestString, $PermitedDomains, False)

_ArrayDisplay($Results)

Func _URL($StringValue, $Domains = "Example", $TestMode = True)
    Local $Pattern = "(?i)\b(?:http://(?:www.)?)?(" & $Domains & ").(?:com|net|org|to)\b"
    Local $Result
    
    $Result = StringRegExp($StringValue, $Pattern)
    ConsoleWrite('Result = ' & $Result & @CRLF)
    
    If $TestMode = False And $Result Then
        $Result = StringRegExp($StringValue, $Pattern, 1)
    EndIf
    
    Return $Result
EndFunc

I love Regular Expressions!! :)

Edited by Zinthose

--- TTFN

Link to comment
Share on other sites

Hi,

I try to make a regular expression to allow only specfic sites to be processed

I just want to make the script allows only:

http://www.example.com

Or

http://example.com

Or

example.com

I tried this but it doesn't work

\b(http://?|www.?)\b\b(zmelody|foxmoviez)\b\b(.com|.net|.org|.to)\b

any idea ??

Thanks in advance

Is not with StringRegExp but work fine:

$TEST1 = "http://www.example.com"
$TEST2 = "http://example.com"

MsgBox(0,"",ReturnAddress($TEST1))
MsgBox(0,"",ReturnAddress($TEST2))

Func ReturnAddress($STRING)
    $SPLIT = StringSplit($STRING,"//",1)
    If IsArray($SPLIT) Then
        If StringLower(StringLeft($SPLIT[2],4)) = "www." Then
            Return StringTrimLeft($SPLIT[2],4)
        Else
            Return $SPLIT[2]
        EndIf
    EndIf
EndFunc

When the words fail... music speaks.

Link to comment
Share on other sites

Hi,

I try to make a regular expression to allow only specfic sites to be processed

I just want to make the script allows only:

http://www.example.com

Or

http://example.com

Or

example.com

I tried this but it doesn't work

\b(http://?|www.?)\b\b(zmelody|foxmoviez)\b\b(.com|.net|.org|.to)\b

any idea ??

Thanks in advance

You could easily make this work with wildcards if you wanted("*"):

$Allowed = "Test2.net|Allowed.com"

$WebsiteURL = "http://www.Allowed.com"


$Allowed = StringRegExpReplace($Allowed, "\.", "\\\.")
$ISA = StringRegExp($WebsiteURL, "(?i)(?:Http://)?(?-i)(?:www\.)?(" & $Allowed & ")", 0)
If $ISA = 1 Then
    MsgBox(0, "", "Allowed")
Else
    MsgBox(0, "", "Not allowed")
EndIf

Seperate with a "|". The parser will automatically just split it up. Try it and let me know!

Link to comment
Share on other sites

Try this...

;## RegEx Web Example
#Include <Array.au3>

Dim Const $PermitedDomains = "zmelody|foxmoviez"
Dim $TestString = "I think example.com is a great site! But then again www.foxmoviez.com " & _
        "is the Shizzle! Not to be confused with that evil site at www.foxmoviez.au. To bad work " & _
        "blocks my access to zmelody at http://www.zmelody.com"

_URL($TestString, $PermitedDomains)

$Results = _URL($TestString, $PermitedDomains, False)

_ArrayDisplay($Results)

Func _URL($StringValue, $Domains = "Example", $TestMode = True)
    Local $Pattern = "(?i)\b(?:http://(?:www.)?)?(" & $Domains & ").(?:com|net|org|to)\b"
    Local $Result
    
    $Result = StringRegExp($StringValue, $Pattern)
    ConsoleWrite('Result = ' & $Result & @CRLF)
    
    If $TestMode = False And $Result Then
        $Result = StringRegExp($StringValue, $Pattern, 1)
    EndIf
    
    Return $Result
EndFunc

I love Regular Expressions!! :)

Thanks

But Unfortunately it accepts:

ww.example.com

w.example.com

tp://w.example.

and any mal formed URL :o

Link to comment
Share on other sites

Is not with StringRegExp but work fine:

$TEST1 = "http://www.example.com"
$TEST2 = "http://example.com"

MsgBox(0,"",ReturnAddress($TEST1))
MsgBox(0,"",ReturnAddress($TEST2))

Func ReturnAddress($STRING)
    $SPLIT = StringSplit($STRING,"//",1)
    If IsArray($SPLIT) Then
        If StringLower(StringLeft($SPLIT[2],4)) = "www." Then
            Return StringTrimLeft($SPLIT[2],4)
        Else
            Return $SPLIT[2]
        EndIf
    EndIf
EndFunc

Thanks

But Unfortunately I cannot control of mal formed URLs

and any mal formed URL

You could easily make this work with wildcards if you wanted("*"):

$Allowed = "Test2.net|Allowed.com"

$WebsiteURL = "http://www.Allowed.com"


$Allowed = StringRegExpReplace($Allowed, "\.", "\\\.")
$ISA = StringRegExp($WebsiteURL, "(?i)(?:Http://)?(?-i)(?:www\.)?(" & $Allowed & ")", 0)
If $ISA = 1 Then
    MsgBox(0, "", "Allowed")
Else
    MsgBox(0, "", "Not allowed")
EndIf

Seperate with a "|". The parser will automatically just split it up. Try it and let me know!

Thanks

But Unfortunately the same problem exists so it accepts:

ww.example.com

w.example.com

tp://w.example.

and any mal formed URL

Link to comment
Share on other sites

Hi,

try this:

Dim Const $PermitedDomains = "zmelody|foxmoviez|example"
Dim $TestString = StringSplit('http://www.example.com,http://example.com,example.com,ww.example.com,w.example.com,tp://w.example,http://w.example.', ',')

For $i = 1 To UBound($TestString) - 1
    _URL($TestString[$i], $PermitedDomains)
Next

Func _URL($StringValue, $Domains = "Example")
    Local $Pattern = '(?<=http://|http://www.)(' & $PermitedDomains & '\.com|net|org|to)|\A(' & $PermitedDomains & '\.com|net|org|to)'
    Local $Result
    $Result = StringRegExp($StringValue, $Pattern, 0)
    ConsoleWrite($StringValue & '  -  Result = ' & $Result & @CRLF)
    Return $Result
EndFunc   ;==>_URL

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Thanks

But Unfortunately it accepts:

ww.example.com

w.example.com

tp://w.example.

and any mal formed URL :)

Tecnically "ww.example.com" and "w.example.com" are valid domains as the sub domain follows the same rules as the root domain's naming convention. That is unless you are truly only interested in the root level domains.

Anyway,

I have a new regular expression and I removed the $AllowedDomains Parameter.. Try this...

;## RegEx Web Example
#Include <Array.au3>

Dim $TestString = "I think ww.example.com tp://w.example example.com is a great site! But then again www.foxmoviez.com is the Shizzle! Not to be confused with that evil site at www.foxmoviez.au. To bad work blocks my access to zmelody at http://www.zmelody.com"

_URL($TestString)

$Results = _URL($TestString, False)

_ArrayDisplay($Results)

Func _URL($StringValue, $TestMode = True)
    Local $Pattern = "(?i)\b(?:http(?:s?)\:\/\/|~/|/)?(?:(?:[-\w]+\.)+(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))\b"
    Local $Result
    
    $Result = StringRegExp($StringValue, $Pattern)
    ConsoleWrite('Result = ' & $Result & @CRLF)
    
    If $TestMode = False And $Result Then
        $Result = StringRegExp($StringValue, $Pattern, 3)
    EndIf
    
    Return $Result
EndFunc
Edited by Zinthose

--- TTFN

Link to comment
Share on other sites

Tecnically "ww.example.com" and "w.example.com" are valid domains as the sub domain follows the same rules as the root domain's naming convention. That is unless you are truly only interested in the root level domains.

Anyway,

I have a new regular expression and I removed the $AllowedDomains Parameter.. Try this...

;## RegEx Web Example
#Include <Array.au3>

Dim $TestString = "I think ww.example.com tp://w.example example.com is a great site! But then again www.foxmoviez.com is the Shizzle! Not to be confused with that evil site at www.foxmoviez.au. To bad work blocks my access to zmelody at http://www.zmelody.com"

_URL($TestString)

$Results = _URL($TestString, False)

_ArrayDisplay($Results)

Func _URL($StringValue, $TestMode = True)
    Local $Pattern = "(?i)\b(?:http(?:s?)\:\/\/|~/|/)?(?:(?:[-\w]+\.)+(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))\b"
    Local $Result
    
    $Result = StringRegExp($StringValue, $Pattern)
    ConsoleWrite('Result = ' & $Result & @CRLF)
    
    If $TestMode = False And $Result Then
        $Result = StringRegExp($StringValue, $Pattern, 3)
    EndIf
    
    Return $Result
EndFunc
Firstly thank you Zinthose for your effort and time

but there's again a problem => the pattern allows : ww.example.com

Posted Image

Actually I work on a script to bypass short protect links almost I finished it except for the pattern that allows it to deal with supported link and link only

It always monitors the clipboard for supported links to start fetching direct link

Edited by drislaam
Link to comment
Share on other sites

Hi,

try this:

Dim Const $PermitedDomains = "zmelody|foxmoviez|example"
Dim $TestString = StringSplit('http://www.example.com,http://example.com,example.com,ww.example.com,w.example.com,tp://w.example,http://w.example.', ',')

For $i = 1 To UBound($TestString) - 1
    _URL($TestString[$i], $PermitedDomains)
Next

Func _URL($StringValue, $Domains = "Example")
    Local $Pattern = '(?<=http://|http://www.)(' & $PermitedDomains & '\.com|net|org|to)|\A(' & $PermitedDomains & '\.com|net|org|to)'
    Local $Result
    $Result = StringRegExp($StringValue, $Pattern, 0)
    ConsoleWrite($StringValue & '  -  Result = ' & $Result & @CRLF)
    Return $Result
EndFunc   ;==>_URL

Mega

Thank you for your help which I actually appreciate it

the problem still exists it allows any malformed links as ht://w.zmelody.com

Edited by drislaam
Link to comment
Share on other sites

Thank you for your help which I actually appreciate it

the problem still exists it allows any malformed links as ht://w.zmelody.com

Really?

#include <Array.au3>
Dim Const $PermitedDomains = "zmelody|foxmoviez|example"
Dim $TestString = StringSplit('ht://w.zmelody.com,http://www.example.com,http://example.com,example.com,ww.example.com,w.example.com,tp://w.example,http://w.example.', ',')

For $i = 1 To UBound($TestString) - 1
    _URL($TestString[$i], $PermitedDomains)
Next

Func _URL($StringValue, $Domains = "Example")
    Local $Pattern = '(?<=http://|http://www.)(' & $PermitedDomains & '\.com|net|org|to)|\A(' & $PermitedDomains & '\.com|net|org|to)'
    Local $Result
    $Result = StringRegExp($StringValue, $Pattern, 0)
    If $Result Then
        ConsoleWrite('! VALID  ' & $StringValue & @TAB & @TAB & @TAB & '  -  Result = ' & $Result & @CRLF)
    Else
        ConsoleWrite('INVALID  ' & $StringValue & @TAB & @TAB & @TAB & '  -  Result = ' & $Result & @CRLF)
    EndIf
    Return $Result
EndFunc   ;==>_URLoÝ÷ Ø·vÚºÚ"µÍSSQËÝËY[ÙKÛÛHHÝ[HÌÌÎÈSQËÝÝÝË^[KÛÛHHÝ[HBÌÌÎÈSQËÙ^[KÛÛHHÝ[HBÌÌÎÈSQ^[KÛÛHHÝ[HBSSQÝË^[KÛÛHHÝ[HSSQË^[KÛÛHHÝ[HSSQËÝË^[HHÝ[HSSQËÝË^[KHÝ[H

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Really?

#include <Array.au3>
Dim Const $PermitedDomains = "zmelody|foxmoviez|example"
Dim $TestString = StringSplit('ht://w.zmelody.com,http://www.example.com,http://example.com,example.com,ww.example.com,w.example.com,tp://w.example,http://w.example.', ',')

For $i = 1 To UBound($TestString) - 1
    _URL($TestString[$i], $PermitedDomains)
Next

Func _URL($StringValue, $Domains = "Example")
    Local $Pattern = '(?<=http://|http://www.)(' & $PermitedDomains & '\.com|net|org|to)|\A(' & $PermitedDomains & '\.com|net|org|to)'
    Local $Result
    $Result = StringRegExp($StringValue, $Pattern, 0)
    If $Result Then
        ConsoleWrite('! VALID  ' & $StringValue & @TAB & @TAB & @TAB & '  -  Result = ' & $Result & @CRLF)
    Else
        ConsoleWrite('INVALID  ' & $StringValue & @TAB & @TAB & @TAB & '  -  Result = ' & $Result & @CRLF)
    EndIf

Mega

I am really sorry

As for your script I read it and tried to use your pattern of my own so it messed up when I tried it

actually I was frustrated of many and many trials as I spend 3 days trying to make this pattern

Any way

Thank you for your appreciated help and please Accept my apology

Edited by drislaam
Link to comment
Share on other sites

I am really sorry

As for your script I read it and tried to use your pattern of my own so it messed up when I tried it

actually I was frustrated of many and many trials as I spend 3 days trying to make this pattern

Any way

Thank you for your appreciated help and please Accept my apology

No problem! :)

So does it finally work for you or do you still need help?

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

No problem! :)

So does it finally work for you or do you still need help?

Mega

Actually it works fine except for it doesn't allow www.example.com

I used it as follow

$catch = ClipGet()
$check = StringRegExp($catch, '(?<=http://|http://www.)(zmelody|foxmoviez|forexshare\.com|net|org|to)|\A(zmelody|foxmoviez|forexshare\.com|net|org|to)', 0)
If $check Then
    MsgBox(0, "", $catch & " is allowed")
Else
    MsgBox(0, "", $catch & " is Not allowed")
EndIf

Any way, Again I thank you very much, for sure your ideas for this pattern should work someway

Link to comment
Share on other sites

Hi,

try this:

#include <Array.au3>
Dim Const $PermitedDomains = "zmelody|foxmoviez|example"
Dim $TestString = StringSplit('www.example.org,ht://w.zmelody.com,http://www.example.org,http://example.com,example.com,ww.example.com,w.example.com,tp://w.example,http://w.example.', ',')

For $i = 1 To UBound($TestString) - 1
    _URL($TestString[$i], $PermitedDomains)
Next

Func _URL($StringValue, $Domains = "Example")
    Local $Pattern = '(?<=http://|http://www.|www.)(' & $PermitedDomains & '\.[com|net|org|to])|\A(' & $PermitedDomains & '\.[com|net|org|to])'
    Local $Result
    $Result = StringRegExp($StringValue, $Pattern, 0)
    If $Result Then
        ConsoleWrite('! VALID  ' & $StringValue & @TAB & @TAB & @TAB & '  -  Result = ' & $Result & @CRLF)
    Else
        ConsoleWrite('INVALID  ' & $StringValue & @TAB & @TAB & @TAB & '  -  Result = ' & $Result & @CRLF)
    EndIf
    Return $Result
EndFunc   ;==>_URL

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Hi,

try this:

#include <Array.au3>
Dim Const $PermitedDomains = "zmelody|foxmoviez|example"
Dim $TestString = StringSplit('www.example.org,ht://w.zmelody.com,http://www.example.org,http://example.com,example.com,ww.example.com,w.example.com,tp://w.example,
http://w.example.', ',')

For $i = 1 To UBound($TestString) - 1
    _URL($TestString[$i], $PermitedDomains)
Next

Func _URL($StringValue, $Domains = "Example")
    Local $Pattern = '(?<=http://|http://www.|www.)(' & $PermitedDomains & '\.[com|net|org|to])|\A(' & $PermitedDomains & '\.[com|net|org|to])'
    Local $Result
    $Result = StringRegExp($StringValue, $Pattern, 0)
    If $Result Then
        ConsoleWrite('! VALID  ' & $StringValue & @TAB & @TAB & @TAB & '  -  Result = ' & $Result & @CRLF)
    Else
        ConsoleWrite('INVALID  ' & $StringValue & @TAB & @TAB & @TAB & '  -  Result = ' & $Result & @CRLF)
    EndIf
    Return $Result
EndFunc   ;==>_URL

Mega

all goes fine except for ht://www.zmelody.com

! VALID  ht://www.zmelody.com             -  Result = 1

The problem always by adding www. to (?<=http://|http://www.)

:)

My first attempt was how to position question mark to make http:// or www. be or not to be considered

All goes fine with any expression as regard to example.com

The challenge always with http:// and www.

Please accepts my repect to your effort in helping me

Edited by drislaam
Link to comment
Share on other sites

LOL,

no problem on that, too

Dim Const $PermitedDomains = "zmelody|foxmoviez|example"
Dim $TestString = StringSplit('ht://www.zmelody.com,www.example.org,ht://w.zmelody.com,http://www.example.org,http://example.com,example.com,ww.example.com,w.example.com,tp://w.example,http://w.example.', ',')

For $i = 1 To UBound($TestString) - 1
    _URL($TestString[$i], $PermitedDomains)
Next

Func _URL($StringValue, $Domains = "Example")
    Local $Pattern = '(?<=http://|http://www.|\Awww.)(' & $PermitedDomains & '\.[com|net|org|to])|\A(' & $PermitedDomains & '\.[com|net|org|to])'
    Local $Result
    $Result = StringRegExp($StringValue, $Pattern, 0)
    If $Result Then
        ConsoleWrite('! VALID  ' & $StringValue & @TAB & @TAB & @TAB & '  -  Result = ' & $Result & @CRLF)
    Else
        ConsoleWrite('INVALID  ' & $StringValue & @TAB & @TAB & @TAB & '  -  Result = ' & $Result & @CRLF)
    EndIf
    Return $Result
EndFunc  ;==>_URL

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

LOL,

no problem on that, too

Dim Const $PermitedDomains = "zmelody|foxmoviez|example"
Dim $TestString = StringSplit('ht://www.zmelody.com,www.example.org,ht://w.zmelody.com,http://www.example.org,http://example.com,example.com,ww.example.com,w.example.com,tp://w.example,http://w.example.', ',')

For $i = 1 To UBound($TestString) - 1
    _URL($TestString[$i], $PermitedDomains)
Next

Func _URL($StringValue, $Domains = "Example")
    Local $Pattern = '(?<=http://|http://www.|\Awww.)(' & $PermitedDomains & '\.[com|net|org|to])|\A(' & $PermitedDomains & '\.[com|net|org|to])'
    Local $Result
    $Result = StringRegExp($StringValue, $Pattern, 0)
    If $Result Then
        ConsoleWrite('! VALID  ' & $StringValue & @TAB & @TAB & @TAB & '  -  Result = ' & $Result & @CRLF)
    Else
        ConsoleWrite('INVALID  ' & $StringValue & @TAB & @TAB & @TAB & '  -  Result = ' & $Result & @CRLF)
    EndIf
    Return $Result
EndFunc ;==>_URL

Mega

oohh! YES, It finally works all fine.

Thank you very very very much Xenobiologist.

It's was really a big dilemma for my script

I have tweaked it to make it doesn't allow uhttp://www.example.com

$check = StringRegExp($catch, '(?<=\Ahttp://|\Ahttp://www.|\Awww.)(zmelody|foxmoviez|forexshare\.[com|net|org|to])|\A(zmelody|foxmoviez|forexshare\.[com|net|org|to])', 0)

BUT All goes to your way of thinking

Thanks again

Link to comment
Share on other sites

Glad to see, you finally got what you want.

Next Regex problem - - - ask me, I like those stuff

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

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...