Jump to content

String Management


Recommended Posts

Hi everyone, I am pretty new to AutoIt.

I have opened a topic here to discuss string management.

I have had a read through the help file under the 'String Management' directory but I still cannot find a way to do what I am going to describe below without using extensive code.

What I want to do is the following. I want to compare the contents of a string to a set of rules.

The string must contain:

1. Less than 32 characters.

2. Only alphanumeric values (e.g Aa-Zz, 0-9). Spaces are also allowed.

3. One of the characters MUST be a letter.

4. However, there are two exceptions to rule no. 2. The string CAN contain also dashes '-' and/or underscores '_'.

Rule 1 is easy. I suppose you could use the StringLen() function.

The rest of the rules I do not know how to account for them in my code.

I suppose you could put all the rules in an If statement with And and Or operators, and everything should work. Then, in the Else section, you could throw an error.

Any plausible ideas as to how to solve this problem? Thanks in advance.

Edited by hallaplay835

_____________________________________________________[size="2"][font="Arial"]"Pain is temporary, glory is forever."[/font][/size]

Link to comment
Share on other sites

  • Moderators

hallaplay835,

Here is my idea of how it might work:

Global $aString[5]

$aString[0] = "123456789 12345678901234567-_A" ; Should pass

$aString[1] = "123456789012345678901234567-_0" ; Should fail - no A-Z

$aString[2] = "123456789012345678901234567890123" ; Should fail - too long

$aString[3] = "123456789 1234567890123456-_A"  ; Should Pass

$aString[4] = "ABCDEFGHIJKLMNOP?URSTUVWXYZ_-"  ; Should fail - contains ?


For $i = 0 To 4
    ; Check length
    If StringLen($aString[$i]) > 31 Then
        ConsoleWrite("Error - String " & $i & " is too long" & @CRLF)
    Else
        ; Check only digits, a-z (case-insensitive), dash, underscore or space
        If StringRegExp($aString[$i], "(?i)[^0-9a-z-_\x20]") Then
            ConsoleWrite("Error - String " & $i & " contains an illegal character" & @CRLF)
        Else
            ; Check there is at least 1 a-z (case-insensitive)
            If Not StringRegExp($aString[$i], "(?i)[a-z]") Then
                ConsoleWrite("Error - String " & $i & " does not contain at least one A-Z character" & @CRLF)
            Else
                ConsoleWrite("Good - String " & $i & " is correct" & @CRLF)
            EndIf
        EndIf
    EndIf
Next

I hope that gives you a start. :unsure:

M23

Edit: Oh dear, George is looking at this thread - donning tin hat and awaiting incoming! :>

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

$sString = "The rest of the rules I do not "
_Match($sString)
$sString = "The rest of the rules I do not know how to account for them in my code"
_Match($sString)

Func _Match($sStr)
    If StringRegExp($sStr, "^[[:alnum:]_-\h]{1,31}$") Then
        MsgBox(4096, "Result", "We have a match")
    Else
        MsgBox(4096, "Result", "We have a problem")
    EndIf
EndFunc

If it's possible for the string to be a complete sentence where it could end in punctuation then I would change the expression to

"^[[:alnum:]_-\h]{1,31}[.?!]$"

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

hallaplay835,

Here is my idea of how it might work:

<snipped>

M23

Edit: Oh dear, George is looking at this thread - donning tin hat and awaiting incoming! :unsure:

You were close but you went the hard way. Classes are good in this situation and with the [:alnum:] class you don't have to set case insensitive.

EDIT:

I don't remember if I have been in touch since the last update but make sure you are using ver:3.0.1.9 of the toolkit.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Hi George,

You don't enforce rule #3.

use a lookbehind assertion

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

Hi George,

You don't enforce rule #3.

use a lookbehind assertion

Possible.

I just ran a quick test at the time and it worked. What did I miss?

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

That the string must contain at least 1 letter

I didn't read the post that he needed it that way. Easy fix as soon as I get a chance to look again.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

StringRegExp() should do it. GEOSoft has a nice tool to test it too

Check post and the link to PCRE Tool Kit in his sig...

Thank you MrMitchell.

_____________________________________________________[size="2"][font="Arial"]"Pain is temporary, glory is forever."[/font][/size]

Link to comment
Share on other sites

hallaplay835,

Here is my idea of how it might work:

Global $aString[5]

$aString[0] = "123456789 12345678901234567-_A" ; Should pass

$aString[1] = "123456789012345678901234567-_0" ; Should fail - no A-Z

$aString[2] = "123456789012345678901234567890123" ; Should fail - too long

$aString[3] = "123456789 1234567890123456-_A"  ; Should Pass

$aString[4] = "ABCDEFGHIJKLMNOP?URSTUVWXYZ_-"  ; Should fail - contains ?


For $i = 0 To 4
    ; Check length
    If StringLen($aString[$i]) > 31 Then
        ConsoleWrite("Error - String " & $i & " is too long" & @CRLF)
    Else
        ; Check only digits, a-z (case-insensitive), dash, underscore or space
        If StringRegExp($aString[$i], "(?i)[^0-9a-z-_\x20]") Then
            ConsoleWrite("Error - String " & $i & " contains an illegal character" & @CRLF)
        Else
            ; Check there is at least 1 a-z (case-insensitive)
            If Not StringRegExp($aString[$i], "(?i)[a-z]") Then
                ConsoleWrite("Error - String " & $i & " does not contain at least one A-Z character" & @CRLF)
            Else
                ConsoleWrite("Good - String " & $i & " is correct" & @CRLF)
            EndIf
        EndIf
    EndIf
Next

I hope that gives you a start. :unsure:

M23

Edit: Oh dear, George is looking at this thread - donning tin hat and awaiting incoming! :>

Thanks a lot Melba23, but I think I prefer GEO Soft's approach using classes. Less code. Anyway, thanks.

_____________________________________________________[size="2"][font="Arial"]"Pain is temporary, glory is forever."[/font][/size]

Link to comment
Share on other sites

$sString = "The rest of the rules I do not "
_Match($sString)
$sString = "The rest of the rules I do not know how to account for them in my code"
_Match($sString)

Func _Match($sStr)
    If StringRegExp($sStr, "^[[:alnum:]_-\h]{1,31}$") Then
        MsgBox(4096, "Result", "We have a match")
    Else
        MsgBox(4096, "Result", "We have a problem")
    EndIf
EndFunc

If it's possible for the string to be a complete sentence where it could end in punctuation then I would change the expression to

"^[[:alnum:]_-\h]{1,31}[.?!]$"

Thank you very much for your answer, GEO Soft. It is all what I expected. However, you do not account for rule 3: the string must contain at least one letter. I am working on that right now. I have also found interesting the toolkit in your sig. Thanks for everything.

_____________________________________________________[size="2"][font="Arial"]"Pain is temporary, glory is forever."[/font][/size]

Link to comment
Share on other sites

Use the pattern with a look-ahead: ^(?=[^[:alpha:]]*[[:alpha:]])[[:alnum:]_-\h]{1,31}$

That assertion is "zero or more non-letters followed by a letter", i.e. "at least one letter". Asserts don't consume anything in the subject, just test the condition.

Contrary to what I wrote in the spoiler few post ago: lookahead is simpler than lookbehind. A lookbehind assertion is restricted to fixed or rather bounded length.

So ^[[:alnum:]_-\h]{1,31}(?=[^[:alpha:]]*[[:alpha:]])$ gives an error due to the * in the assert (unbounded length)

But rewriten this way, it works: ^[[:alnum:]_-\h]{1,31}(?=[^[:alpha:]]{0,30}[[:alpha:]])$

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