Jump to content

StringRegExp help


Recommended Posts

  • Moderators

ShadowElf,

What have you tried so far? :D

What is the precise format you want? Is it a series of any number of letters, followed by either nothing or the digits "093"?

M23

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

format

ONLY LETTERS or

FIRST letter and after numbers

examples

asdzc -> OK

asdjasd02389 -> ok

231321asd -> NOT OK

90423j)(* -> NOT OK

asd90asd90 -> OK

asd*& -> NOT ok

max 20 chars

I like IT: php, mysql, codeingiter, css, jquery and AUTOIT

Link to comment
Share on other sites

  • Moderators

Thanubis,

Nice.

I have to keep reminding myself that just because you can do something with a RegEx, it does not always make sense to actually do so.

If you are not a guru in the blasted things, it is ofter easier (and faster!) to use other functions. :D

M23

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

format

ONLY LETTERS or

FIRST letter and after numbers

examples

asdzc -> OK

asdjasd02389 -> ok

231321asd -> NOT OK

90423j)(* -> NOT OK

asd90asd90 -> OK

asd*& -> NOT ok

max 20 chars

From "ONLY LETTERS or FIRST letter and after numbers" one may translate it into:

"one or more [english] letter (casing?) followed by zero or more [decimal] digits" in the limit of 20 characters.

The 20c limit is easy:

If StringLength($str) > 20 Then ... $str is rejected , or truncated

The condition above would translate in turn into the regexp:

[a-z]+\d*

If casing should be ignored, make it (?i)[a-z]+\d* or [a-zA-Z]+\d*

Only catch: this doesn't cope with one of your examples:

asd90asd90 -> OK

So either your actual rules are in fact more complex than you said, or this example is invalid. Only you can read your mind :D

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

$s = "oidfgdgu73456548"

$r = StringRegExp($s, "\D+\d*", 1)

If IsArray($r) and $r[0] = $s Then
    ConsoleWrite($s & "< Allowed" & @CRLF)
Else

    ConsoleWrite($s & "< Not Allowed" & @CRLF)
EndIf

EDIT: Added check for return being an array

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

$r = StringRegExp($s, "\D+\d*", 1)

Hi Martin,

Sorry but this doesn't work as \D allows non-letters & non-digits ahead: $str = "$got you!159" would incorrectly match.

[:alpha:] or [a-z] wouldn't tolerate that.

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

One of the many ways you could do this using StringRegExp() if you wanted 

Global $aTest[6]
$aTest[0] = "asdzc"; -> OK
$aTest[1] = "asdjasd02389"; -> ok
$aTest[2] = "231321asd"; -> NOT OK
$aTest[3] = "90423j)(*"; -> NOT OK
$aTest[4] = "asd90asd90"; -> OK
$aTest[5] = "asd*&"; -> NOT ok

For $i = 0 To  5
    If StringRegExp($aTest[$i],"^[[:alpha:]][[:alnum:]]{0,19}{:content:}quot;) Then
        MsgBox(0,"Test Result" , $aTest[$i] & " is valid")
    Else
        MsgBox(0,"Test Result" , $aTest[$i] & " is not valid")
    EndIf
Next

Explanation of the regex pattern

"^[[:alpha:]][[:alnum:]]{0,19}{:content:}quot;

«^»                             Assert position at the beginning of a line (at beginning of the string or after a line break character) 

«[[:alpha:]]»               Match a single character in the range A to Z or a to z 

«[[:alnum:]]{0,19}»   Match a single character present in the range A to Z or a to z or 0 to 9 as many times as posible, between zero and 19 times 

«$»                             Assert position at the end of a line (at the end of the string or before a line break character) i.e. make sure all characters are include in the match 

Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

And another attempt.

Local $sMessage, $sResults
Local $aTestString[8][2] = [["asdzcasddzc", "OK"],["asdjasd02389", "OK"],["asd90asd90", "OK"], _
        ["231321asd", "NOT OK"],["90423jr)(*", "NOT OK"],["asdasdasd*&", "NOT OK"], _
        ["asd90asd90asd", "NOT OK"],["asdzcasddzc12345678910", "NOT OK"]]

For $i = 0 To UBound($aTestString) - 1

    $sMessage = "NOT OK"

    If (StringRegExp($aTestString[$i][0], "^[a-z]") And StringRegExp($aTestString[$i][0], "\d$")) And _ ; If start with letter & end with digit - ok
            StringLen($aTestString[$i][0]) <= 20 Or _       ; If less than or equal to 20 characters - ok
            (StringRegExp($aTestString[$i][0], "[^a-z]") = 0) Then $sMessage = "OK" ; If only letters - ok

    $sResults &= $aTestString[$i][0] & @TAB & $aTestString[$i][1] & " expected. Result is " & $sMessage & @CRLF

Next

MsgBox(0, "Results", $sResults)
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...