Jump to content
MyEarth

[REGEX] Complex string to be validated

Recommended Posts

MyEarth

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
jchd

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

    • gruntydatsun
      By gruntydatsun
      I have an XML file and every time there are three lines in a row with only <null/> in them, i want to insert a fourth line with <null/>.   Each line starts with 3 white spaces, followed by <null/> and ends with a white space followed by CR LF.   The presence of the three lines as described is unique to the points where I want to insert a line in this document.
       I'm trying to figure out how to apply the repeating part of a regex  {1,4} but apply it to this whole segment. 
      So far I have the below which picks up an individual line ok:
      ^\s{3}<null/>\s\r\n I tried wrapping it all in braces () then adding {3} but I'm obviously getting something wrong. 
      Attached is a section from the xml file with a block of nulls that should be matched if anyone would like to have a look.
      Help_From_Forum.xml
    • milkmoron
      By milkmoron
      I am trying to search in a web browser dates XX/XX/XXXX that are also links. I want to click them after and remove them from the array. This is all I have so far. Nothing shows up. What am I doing wrong?
      ControlFocus ("Customer Center", "", "")
      Local $aArray = StringRegExp('(..)/(..)/(....)', '(..)/(..)/(....)', $STR_REGEXPARRAYFULLMATCH)
      For $i = 0 To UBound($aArray) - 1
          MsgBox($MB_SYSTEMMODAL, "RegExp Test with Option 2 - " & $i, $aArray[$i])
      Next
       
    • WoodGrain
      By WoodGrain
      Hi All,
      I'd like to replace 'COMMA' with ',' for example:
      $myString = "COMMA" StringRegExpReplace($myString, 'COMMA', ',') Now I've tried escaping the ',' in various ways unsuccessfully, such as:
      '[,]'
      "[,]"
      '\,'
      [,] seems to work in the pattern, I just can't figure out how to use it in the replace, and it seems everyone online is only interested in removing/replacing commas lol.
      I also tried creating and using a variable as the replacement but also didn't work:
      $myComma = "," $myString = "COMMA" StringRegExpReplace($myString, 'COMMA', $myComma) I'm sure it's super simple if someone could point me in the right direction - thanks.
    • rcmaehl
      By rcmaehl
      Hi all,
      I still suck at regex as always and I need some help. According to the regex tester I normally use this should be working fine but it doesn't....
      StringRegExp($sString, "\A[1-9]+[0-9]*(\-[1-9]+[0-9]*)?,*\Z") I basically want to match:
      all numbers EXCEPT 0, but including 10, 20, etc with each number separated by a comma and allowing a "-" separated range as a value For example:
      1-5,7,10-12 I've spent a couple hours modifying it but I'm not sure where I've gone wrong. Any help would be appreciated!
    • ISI360
      By ISI360
      Hi!

      I need a little bit help from some RegEx experts please:
      I would make my ISN AutoIt Studio faster when generating the scripttree. And what would be better to do this via regex?
      Problem is i am not really good at this regex stuff. So maybe someone could help me here.
       
      The challange is to get all Global Variables from a script via RegEx in a Array.
      Here is a example script with some tests:
      Global $Var1 = 1234 Local $Local_Var = 1234 $Ignore_me_too = 1234 Global $Var2 = 1234, $var3 = 1242 Global $ahIcons[30], $ahLabels[30] Global Const $Var4 = iniread($inivar1,"jj","jj","") , $var5= iniread($inivar2,"jj","jj","") Global $Var_String = "was" Global $Array_Test[16] = [1,15,16,0,31,15,25,15,25,30,8,30,8,15,1,15] Global Enum $MARGIN_SCRIPT_NUMBER = 0, $MARGIN_SCRIPT_ICON, $MARGIN_SCRIPT_FOLD Global Const $Delim = '\', $Delim1 = '|' Global $hard1 = "a", _ $hard2 = "b", _ $hard3 = "c"  
      The returning array should look like this:
      $Var1 $Var2 $var3 $Var4 $var5 $Var_String $Array_Test $MARGIN_SCRIPT_NUMBER $MARGIN_SCRIPT_ICON $MARGIN_SCRIPT_FOLD $Delim $Delim1 $hard1 $hard2 $hard3  
      I already made some success with a expression i found in the SciTE Jump Tool:  (\$\w+)(?:[\h\[.=+*/^,)\-])?
      This nearly returns the perfect results. But it does not check if it´s a global variable (with the const and enum options) and also returns variables in commands (for example $inivar1)
      I also found this regex: (?im:^(?=Global|Const|Enum|Static)(?:Global)?\h*(?:Const|Enum|Static)?(?:(?<=Enum)\h+Step\h+[+*-]\d+)?\h*)([^\r\n .\=]+)
      This returns also usefull results...but trying to understand this explodes my head

      Maybe someone can help me here?
      Thanks in advance!
×