Jump to content

Validate User Input Syntax (StringRegExp)


Go to solution Solved by jchd,

Recommended Posts

hi world :)
 

I am trying to validate user input as valid syntax

proper syntax is <any number><single letter>

single letter can only be d,w,m,q,y (for day, w, month, quarter, year)

this is what i have so far but not quite working 

$user_input = "499d" ;valid
$user_input = "499dd" ;not valid because syntax is only 1 letter
$user_input = "499r" ;not valid because character is not d,w,m,q,y
$user_input = "d334" ;not valid because syntax is number then letter
$user_input = "xyz" ;not valid because syntax is number then letter

$user_input = StringRegExp(StringStripWS($user_input, 8), "([0-9]*[d,w,m,q,y])", 3)

would like to provide an error if the syntax is not valid

thank you in advance!!

Link to comment
Share on other sites

  • Solution

Try this:

Local $a = ["456w", "465dd", "456", "y45", "dwm"]

For $s In $a
    $valid = StringRegExp(StringStripWS($s, 8), "^\d+[dwmqy]$")
    ConsoleWrite($s & " is " & ($valid ? "" : "in") & "valid" & @LF)
Next

If you accept uppercase letters as well, use this pattern instead: "(?i)^[0-9]+[dwmqy]$"

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

the syntax checking works great  thanks again Jchd!  however, i am unable to single out each min/max volume when im trying to read input from a text file.  was working but noticing its not consistently working.

sample input - each value is in its own line (could be with or without spaces min: 5d or min:5d😞

min: 5m
max: 34d

this is what i am doing to get each of the values but not consistently working:

$min_value = StringRegExp(StringStripWS($expecting, 8), "\min:([0-9a-z]*[^max:])", 3)
$max_value = StringRegExp(StringStripWS($expecting, 8), "\max:([0-9a-z]*)", 3)

what i am expecting is 

$min_value = 5d
$max_value = 34m

thank you again in advance!

Edited by gcue
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...