Jump to content
Ghost1982

RegEx - display string that DOESN'T match a pattern

Recommended Posts

Hello,

I was wondering if it's possible to somehow display the pattern that DOESN'T follow the regex pattern from string. So it scans the string according to pattern, detects that it's wrong and additionally display the wrong part of name.

 

Let's say I have a ReGex that scans the website address www.some_address.com

 

(?i) ((www\.) ([0-9a-z]{1,}) (\.com$)

 

When someone enters www.some_address.NET or ww.some_address.com, I would like to display MsgBox or SplashTextOn text field with message:

 

"Address cannot end with NET"

or

"Address cannot start with ww."

 

 

Share this post


Link to post
Share on other sites

Hello,

Functions will do what they are designed to do so you can't make them do something else :D

However, you can use StringLeft and StringRight functions to get the first 4 characters, see if they match "www." and the last 4 to see if they match ".com".

Also if you explain us what you're trying to achieve with your script using that url, maybe we can help you better ;)

 

Edited by Neutro

Share this post


Link to post
Share on other sites

Basically a regular expression checks if something matches or not. To verify if three different parts match, you'll probably need to use multiple regular expressions. In this case, I would break it up into sections:

ValidateWebsite("ww.some_.address.net")
ValidateWebsite("www.some_.address.net")
ValidateWebsite("www.some_.address.com")
ValidateWebsite("www.some_address.com")

Func ValidateWebsite($sWebsite)

    ; If the website doesn't start with www.
    If Not StringRegExp($sWebsite, "(?im)^www\..*") Then
        ConsoleWrite("Fail 1" & @CRLF)
    ; If the website doesn't end with .com
    ElseIf Not StringRegExp($sWebsite, "(?i)^www\..*\.com$") Then
        ConsoleWrite("Fail 2" & @CRLF)
    ; If the website doesn't match in the middle
    ElseIf Not StringRegExp($sWebsite, "(?i)^www\.[^.]+\.com$") Then
        ConsoleWrite("Fail 3" & @CRLF)
    Else
        ConsoleWrite("Correct" & @CRLF)
    EndIf

EndFunc

A few points on your RegEx:

  1. {1,} is the same as +
  2. You have an extra opening parenthesis and extra spaces (I think this is due to copy-paste)
  3. [0-9a-z]+ doesn't match some_address, you probably want to match everything except periods: [^.]+

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites
2 hours ago, seadoggie01 said:

Basically a regular expression checks if something matches or not. To verify if three different parts match, you'll probably need to use multiple regular expressions. In this case, I would break it up into sections:

ValidateWebsite("ww.some_.address.net")
ValidateWebsite("www.some_.address.net")
ValidateWebsite("www.some_.address.com")
ValidateWebsite("www.some_address.com")

Func ValidateWebsite($sWebsite)

    ; If the website doesn't start with www.
    If Not StringRegExp($sWebsite, "(?im)^www\..*") Then
        ConsoleWrite("Fail 1" & @CRLF)
    ; If the website doesn't end with .com
    ElseIf Not StringRegExp($sWebsite, "(?i)^www\..*\.com$") Then
        ConsoleWrite("Fail 2" & @CRLF)
    ; If the website doesn't match in the middle
    ElseIf Not StringRegExp($sWebsite, "(?i)^www\.[^.]+\.com$") Then
        ConsoleWrite("Fail 3" & @CRLF)
    Else
        ConsoleWrite("Correct" & @CRLF)
    EndIf

EndFunc

A few points on your RegEx:

  1. {1,} is the same as +
  2. You have an extra opening parenthesis and extra spaces (I think this is due to copy-paste)
  3. [0-9a-z]+ doesn't match some_address, you probably want to match everything except periods: [^.]+

Hello

 

Thanks. This will be helpful. I know there may be errors in my RegEx. I was just writing it from my head without compiling.

 

What I'm trying to achieve is to make some pop up with information for user what is wrong in the string and to type it again properly. We never know what can be typed incorrectly by someone but I'd like to show that particualr incorrect part of address that was entered. Not only ".net" or "ww".

 

Share this post


Link to post
Share on other sites

The absolute way to test for a valid URL is :

#include <Constants.au3>

MsgBox ($MB_SYSTEMMODAL,"",_IsValidURL ("https://www.autoitscript.com/forum/topic/201773-regex-display-string-that-doesnt-match-a-pattern/"))
MsgBox ($MB_SYSTEMMODAL,"",_IsValidURL ("https;//www.autoitscript.com/forum/topic/201773-regex-display-string-that-doesnt-match-a-pattern/"))
MsgBox ($MB_SYSTEMMODAL,"",_IsValidURL ("http://www.autoitscript.net/forum/topic/201773-regex-display-string-that-doesnt-match-a-pattern/"))
MsgBox ($MB_SYSTEMMODAL,"",_IsValidURL ("https://ww.autoitscript.com/forum/topic/201773-regex-display-string-that-doesnt-match-a-pattern/"))

Func _IsValidURL ($sURL)
  Return InetGetSize ($sURL) > 0
EndFunc

 

Share this post


Link to post
Share on other sites
1 hour ago, Nine said:

The absolute way to test for a valid URL is :

#include <Constants.au3>

MsgBox ($MB_SYSTEMMODAL,"",_IsValidURL ("https://www.autoitscript.com/forum/topic/201773-regex-display-string-that-doesnt-match-a-pattern/"))
MsgBox ($MB_SYSTEMMODAL,"",_IsValidURL ("https;//www.autoitscript.com/forum/topic/201773-regex-display-string-that-doesnt-match-a-pattern/"))
MsgBox ($MB_SYSTEMMODAL,"",_IsValidURL ("http://www.autoitscript.net/forum/topic/201773-regex-display-string-that-doesnt-match-a-pattern/"))
MsgBox ($MB_SYSTEMMODAL,"",_IsValidURL ("https://ww.autoitscript.com/forum/topic/201773-regex-display-string-that-doesnt-match-a-pattern/"))

Func _IsValidURL ($sURL)
  Return InetGetSize ($sURL) > 0
EndFunc

 

But here we're still analysing only "ww" or "net". These were just example strings that someone can enter. Someone may also type w.autoitscript.com or abc.autoitscript.xyz or anything else. What I'm trying to achievie is to display ANY wrong part of the string.

 

So the ONLY correct address in this case is www.autoitscript.com. If any part of it is mispelled I'd like to display this "wrong part" that DOESN'T match the RegEx pattern. Is it possible?

Share this post


Link to post
Share on other sites

If you break up the task, kind of, as I showed... but you won't really know, as garbage in = garbage out 😐

You can check the smaller pieces with a regular expression, but the real way to know is to try


All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites
4 hours ago, Ghost1982 said:

What I'm trying to achieve is to make some pop up with information for user what is wrong in the string

Regex is a good way. You might use it first to split the address to an array and then successively check the array elements to build the error message(s)

#Include <Array.au3>

$str = "www.so.me_add.ress.com"
$r = StringRegExp($str, '^(\w+)\.(.+)\.(\w+)$', 3)
_ArrayDisplay($r)

:)

Share this post


Link to post
Share on other sites

if you have all the rules, you could also SRER the matches to blanks, leaving only the wrong portion...

edit: is it just 3 w's and ends in .com?  just refining this behavior?

$err = ""

;~ $str = "www.so.me_add.ress.com"
;~ $str = "www.so.me_add.ress.net"
$str = "w3.so.me_add.ress.org"
If stringleft($str , 4) <> "www." Then $err &= stringleft($str , stringinstr($str , ".")) & @CRLF
If stringright($str , 4) <> ".com" Then $err &= stringright($str , 4)

$err <> "" ? msgbox(0, 'error' , $err) : msgbox(0,  'Good' , $str)

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...