Jump to content

~ A simple advice using StringRegExp please? (SOLVED)


EKY32
 Share

Recommended Posts

No one can say that the command StringRegExp is easy to use :S

I could understand it but not successfully use it, so i tried to use it in my script to check for Valid User ID (wich should be a name of a folder) so i only want numbers, dot (.) dash (-) and Underscore (_) to be avaliable in the ID, and it should be pure English characters so here is my script but i'm having a problem with excluding quotes (') and double quotes ("") from the list of forbidden characters, and also other (ascii) ones, I know it could be better some how and hopfully asking for help.

Thank you.

Func _IsValid($Text)
    If $Text <> "" Then
        If StringLen($Text) < 26 And StringLen($Text) > 4 Then
            If Not StringRegExp($Text, "[\\]|[@:!#?$%^,<>~&amp;*/()+=}{]|[\]]|[\[]|[\s]|[\###]|[\Q]") Then
                ; Here i excluded the (.-_) from the list just to check if they are repeated only once
                ; I know this is possible with StringRegExp using {x} but i couldn't use it successfully
                If StringInStr($Text, ".") Then
                    $Dots = StringSplit($Text, ".")
                    If $Dots[0] > 2 Then Return $ERROR_REG_DOTS
                EndIf
                If StringInStr($Text, "-") Then
                    $Dashes = StringSplit($Text, "-")
                    If $Dashes[0] > 2 Then Return $ERROR_REG_DASHES
                EndIf
                If StringInStr($Text, "_") Then
                    $Underscores = StringSplit($Text, "_")
                    If $Underscores[0] > 2 Then Return $ERROR_REG_UNDERSCORES
                EndIf
                $All = StringReplace($Text, ".", "")
                $All = StringReplace($Text, "-", "")
                $All = StringReplace($Text, "_", "")
                $All = StringRegExpReplace($Text, "[\d]", "")
                If StringIsUpper($All) Or StringIsLower($All) Then Return 1 ; This is the way i used to check if the $Text is pure English characters
            Else
                Return $ERROR_REG_INVALID_CHR
            EndIf
        Else
            Return $ERROR_REG_LENGTH
        EndIf
        Return 1
    Else
        Return $ERROR_REG_EMPTY
    EndIf
EndFunc   ;==>_IsValid
Edited by EKY32

[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

Link to comment
Share on other sites

  • Moderators

If Not StringRegExp($s_username, "^[w_-.]+z") Then
  ; invalid user
EndIf

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

If Not StringRegExp($s_username, "^[w_-.]+z") Then
; invalid user
EndIf

:huh: awesome!

Thank you Mr. SmOke_N

But still giving me an error when the $s_username is just English chars.

Edited by EKY32

[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

Link to comment
Share on other sites

What's the error and what are you doing to cause the error?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

This shouldn't give me the "Invalid Username" message because all the chrars are available to use:

If Not StringRegExp("user.-_4", "^[w_-.]+z") Then MsgBox(0,"","Invalid username")

I need to allow the user to enter only English chars, numbers, a dot, dash or underscore.

[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

Link to comment
Share on other sites

I don't get it.

I just created a user account named 'ƱʞΞὟ⁉₍₈₎∋∺' and Vista has no problem with it.

Only a few characters from the whole Unicode range are not allowed as user name (and folder name).

Unless there is something else that I'm missing, your character range is way too narrow in the general case.

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

Anyway, I just need the English characters, and numbers and 1 dot or 1 dash or 1 underscore to be available for the user to insert in the Input Control.

[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

Link to comment
Share on other sites

Then make that:

If Not StringRegExp("user.-_4", "^[w_-.]+z") Then MsgBox(0,"","Invalid username")

Hint: the hyphen (dash) has a special meaning inside a character class, meaning a range like in a-z (from a to z). Escaping it works as you want AFAICT.

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

  • Moderators

I have absolutely no idea what you mean by "English" characters.

But try this:

If Not StringRegExp("user.-_4", "(?i)^[0-9a-z.-_]+z") Then MsgBox(0,"","Invalid username")

There is one that doesn't need to be escaped, but it should explain enough for you to understand.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I have absolutely no idea what you mean by "English" characters.

I'm sorry for the bad expression, i mean do deny the user typing any strings from other languages like (Russian or Arabic...)

any way, thank you all, the last one works fine, and I'm sorry but StringRegExp is really not that easy to understand <_<

Thank you.

[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

Link to comment
Share on other sites

Further notes, in part dictated by SmOke_N reply (eh, beat you on the line on this one!).

Specifying _ in the class is redundant since w includes it anyway.

Dot (.) doesn't have to be escaped in a class.

Current AutoIt PCRE support does not use UCP (Unicode Character Properties) compile option, meaning that w is equivalent to [A-Za-z0-9_], but that may change someday (I strongly hope so). If that ever happens, then your regexp would allow anything that Unicode classifies as letter, which is clearly not what you want.

Coding defensively against this possibility imply using [A-Za-z0-9_-.] as your basic pattern class.

About your requirement that at most one dot and/or dash and/or underscore is/are present in the string and unless you're ready to embark into a huge ugly pattern, I'm afraid you need to use classical String* functions to check that in a pedestrian way.

Final personal remark: there are other means to stand out on this forum than by prepending '~ ' to the title of every post you make here.

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

About your requirement that at most one dot or dash or underscore is/are present in the string and unless you're ready to embark into a huge ugly pattern, I'm afraid you need to use classical String* functions to check that in a pedestrian way.

I'd prefer to use StringInStr to identify the error and tell the user about it. :)

$Reg_Err_Msg = _IsValid(GUICtrlRead($Reg_ID), $V_ID)
If $Reg_Err_Msg = 1 Then
 RegAvailable()
ElseIf $Reg_Err_Msg = $ERROR_REG_EMPTY Then
SetCtrlRegStatus($Er_ID, "Please type a valid ID", $Reg_ID)
ElseIf $Reg_Err_Msg = $ERROR_REG_LENGTH Then
SetCtrlRegStatus($Er_ID, "The ID length must be more than '4' characters "&@CRLF&"and less than '26' characters", $Reg_ID)
ElseIf $Reg_Err_Msg = $ERROR_REG_INVALID_CHR Then
SetCtrlRegStatus($Er_ID, "Only dot (.) dash (-) and underscore (_) are " & @CRLF & "avaliable in the ID", $Reg_ID)
ElseIf $Reg_Err_Msg = $ERROR_REG_DOTS Then
SetCtrlRegStatus($Er_ID, "Only 1 dot (.) is avaliable in the ID", $Reg_ID)
ElseIf $Reg_Err_Msg = $ERROR_REG_DASHES Then
SetCtrlRegStatus($Er_ID, "Only 1 dash (-) is avaliable in the ID", $Reg_ID)
ElseIf $Reg_Err_Msg = $ERROR_REG_UNDERSCORES Then
SetCtrlRegStatus($Er_ID, "Only 1 underscore (_) is avaliable in the ID", $Reg_ID)
EndIf

Thank you.

[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

Link to comment
Share on other sites

A safe bet!

Good luck.

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