MyEarth

[REGEX] Complex string to be validated

2 posts in this topic

#1 ·  Posted (edited)

Hello, i need to validate a string can be different things. I just need a True - False return value, no groups or things like that. It will be always one line at time to be processed by StringRegEx

Valid:

13:52|String
02:52 XX|String
13:52~SUN, MON, TUE, WED, THU, FRI, SAT|String
02:52 XX~SUN, MON, FRI|String
22/04/2017 13:52|String
22/04/2017 02:52 YY|String

Not Valid
22/04/2017 13:52~Dom|String

I need to validate until and inclusively the | after that i don't care

The XX and YY value are two $sVariable from my script

SUN, MON, TUE, WED, THU, FRI, SAT are fixed value, the can be mixed but always in the same order like

SUN

SUN, TUE, WED

SUN, SAT 

The time can be 12 or 24 hours, the date is always in the same format DD/MM/YYYY. If there is a date can't be a day after that ( see not valid )

Well i think is all :D

Sorry if i don't provide a working code, regex is too way complex.

Thanks

Edited by MyEarth

Share this post


Link to post
Share on other sites



#2 ·  Posted

Try this:

Local $aIn = [ _
    "13:52|String", _
    "02:52 XX|String", _
    "13:52~SUN, MON, TUE, WED, THU, FRI, SAT|String", _
    "22/04/2017 13:52|String", _
    "22/04/2017 02:52 YY|String", _
    "22/04/2017 13:52~Dom|String", _
    "13:72|String", _
    "33:12|String", _
    "13:52~ WED, THU|String", _
    "22/14/2017 13:52|String", _
    "62/04/2017 13:52|String", _
    "22/04/2117 13:52|String" _
]

Local $sVarXX = "XX", $sVarYY = "YY"

; Let's avoid painful repetition of sub patterns
;
; (?(DEFINE) (?<Time>   (?:[01]\d | 2[0-3]) : [0-5]\d ) )                   ; subroutine for hh:ss
; (?(DEFINE) (?<Day>    SUN | MON | TUE | WED | THU | FRI | SAT) )          ; subroutine for one day name
; (?(DEFINE) (?<Daylist> (?&Day) (?:,\ (?&Day))* ) )                        ; subroutine for a list of days
; (?(DEFINE) (?<Date>   (?:[0-2]\d|3[0-1]) / (?:0\d|1[0-2]) / 20\d\d\ ) )   ; subroutine for dd/mm/20xx (I choose to restrict YYYY to 2000..2099 but your app may need otherwise)
;
; Now we can sew all the pieces together, in the order of cases in $aIn
;
; (?x) allows ignoing non-escaped whitespaces in the pattern for improved readability
;
;"(?x) ^                                            ; anchor at start of string
;        (                                          ; group the pieces ORed (outer alternation)
;        (?&Time)                                   ; time alone
;        |                                          ; or
;        (?&Time) \ \Q" & $sVarXX & "\E             ; time space XX
;        |                                          ; or
;        (?&Time) ~ (?&Daylist)                     ; time tilde daylist
;        |                                          ; or
;        (?&Date) (?&Time) \                        ; date space time
;        |                                          ; or
;        (?&Date) (?&Time) \ \Q" & $sVarYY & "\E    ; date time space XX
;        )                                          ; end of alternation
;\|"                                                ; final bar
;
; Smoke test:

Local $sPattern = _
    "(?x)" & _
    " (?(DEFINE) (?<Time>   (?:[01]\d | 2[0-3]) : [0-5]\d ) )" & _
    " (?(DEFINE) (?<Day>    SUN | MON | TUE | WED | THU | FRI | SAT) )" & _
    " (?(DEFINE) (?<Daylist> (?&Day) (?:,\ (?&Day))* ) )" & _
    " (?(DEFINE) (?<Date>   (?:[0-2]\d|3[0-1]) / (?:0\d|1[0-2]) / 20\d\d\ ) )" & _
    "" & _
        "^" & _
        " (?:" & _
            " (?&Time)" & _                                     ; time alone
            " | " & _                                           ; or
            " (?&Time) \ \Q" & $sVarXX & "\E" & _               ; time space XX
            " | " & _                                           ; or
            " (?&Time) ~ (?&Daylist)" & _                       ; time tilde daylist
            " | " & _                                           ; or
            " (?&Date) (?&Time)" & _                            ; date time
            " | " & _                                           ; or
            " (?&Date) (?&Time) \ \Q" & $sVarYY & "\E" & _      ; date time space XX
        ")" & _
    "\| .*"


For $s In $aIn
    ConsoleWrite('"' & $s & '" is ' & (StringRegExp($s, $sPattern) ? '' : 'in') & 'valid' & @LF)
Next

 


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)

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

  • Similar Content

    • TheAutomator
      By TheAutomator
      Can anyone tell me why this isn't working?..
      #include <array.au3> $regexp = StringRegExp("test 'a b c'", "'([^']|'')*'|\S+", 3) _ArrayDisplay($regexp) trying to split this "test 'a b c'  'some other '' test'' ...'" into:
      0: test
      1: 'a b c'
      2: ...
      but it gives me:
      0: test
      1: c
    • anthonyjr2
      By anthonyjr2
      Hi guys,
      I am pretty bad with regex, and am having some trouble trying to come up with an expression for a certain type of string. Basically I want to be able to tell if a string is of the format:
      AA#####A
      Where the A's are any letter from A-Z and the #'s are any digit from 0-9.
      I've been playing around with a regex tester online for a while but I can't really seem to grasp the concept very well. Could anyone give me any tips?
      This isn't exactly an AutoIt specific question which is why I didn't post it in General Help & Support.
    • tezhihi
      By tezhihi
      I have a file (see attached file) with a string all line and this problem on here is I want to separate all $00:, $03:, $10:, $20:, $25:, $30:, $40:, $45:, $110:, $115:, $120: and $T. It's mean that each $ with value start a new line ( a new paragraph). I tried with Regular Expression in notepad++ ex:
      Find ($00:, $01:, $03: and so on) with regex (\$)([0-9]+): and replace is \r\n\1\2 (I think \r\n is @CRLF (not sure :() ) Find $T with regex (\$T)(.*?)(\$T) and replace is \1\2\r\n\3 When I try these regex to replace in notepad on StringRegexReplace the results is incorrect . I have read some example simple about regex. Please advise me how to do that with some example on autoit . The result will be in attached photo. Thanks 
      ahihi.txt

    • DavidLago
      By DavidLago
      Hello. 
      I need to list only the folders that has a name composed of numbers only.
      How do I do this?
      OBS: I just posted the relevant content to the folder.
      Global $path = "G:\jobs\" For $i = 1 To $aArray[0] Local $search = FileFindFirstFile($path & $aArray[$i] & "\[0-9]" ) I tried with "\*.*" and it returns me the expected result, but the other doesn't.
    • nikink
      By nikink
      Hi all, it's been a while since I last used regular expressions and I find myself out of time to experiment with this particular issue, so I throw myself upon your mercy and expertise.
      I am looking to create a function that will say whether or not a supplied string is a valid UUID or not.
      Local $sTestF = '4C4C4544-004A-4C10-8054-B7C04F46343' Local $sTestT = '4C4C4544-004A-4C10-8054-B7C04F463432' ConsoleWrite('False = ' & _IsValidUUID($sTestF) & @CRLF) ConsoleWrite('True = ' & _IsValidUUID($sTestT) & @CRLF) Func _IsValidUUID($sUUID) ;[\p{XDigit}]{8}-[\p{XDigit}]{4}-[34][\p{XDigit}]{3}-[89ab][\p{XDigit}]{3}-[\p{XDigit}]{12} ; Test UUID = '4C4C4544-004A-4C10-8054-B7C04F463432' Local $sRegExp = '([:xdigit:]){8}\-([:xdigit:]){4}\-([34])([:xdigit:]){3}\-([89ab])([:xdigit:]){3}\-([:xdigit:]){12}' ConsoleWrite(StringRegExp($sUUID, $sRegExp) & @CRLF) Local $Result = StringRegExp($sUUID, $sRegExp) ConsoleWrite($Result & @CRLF) If @error Then ConsoleWrite('Error: [' & @error & ']' & @CRLF) Return 'False' Else ConsoleWrite('Error2: [' & @error & ']' & @CRLF) Return 'True' EndIf EndFunc In the line under the Function call, you'll see the regex I found to do this from a google search. That was my starting point, and I'm trying to get it to work in Au3 and failing miserably.
      $sTestF is a known invalid String
      $sTestT is a known valid String
      Everything I've tried so far has produced the same results for both.
      Any help you could provide me is greatly appreciated. Thanks for your time!