Jump to content

Reg. Expression


Idea
 Share

Recommended Posts

If I have multiple string such as this: 2005-09-19 08:25:12 how can I validate the data via regular expressions and then add up the times? At the moment I'm doing a character by character check and then using some time functions on groups of characters but this is lines and lines of code! I've never used regular expressions although I know I will be using them quite soon. Are they a viable solution to my problem? Thanks. ;)

Link to comment
Share on other sites

If I have multiple string such as this: 2005-09-19 08:25:12 how can I validate the data via regular expressions and then add up the times? At the moment I'm doing a character by character check and then using some time functions on groups of characters but this is lines and lines of code! I've never used regular expressions although I know I will be using them quite soon. Are they a viable solution to my problem? Thanks. ;)

i think they would be good for you, sorry i can't offer specific usage advice, i personally don't use them. one of the first languages i played with was perl, and i didn't even like them then. :P
Link to comment
Share on other sites

  • Developers

If I have multiple string such as this: 2005-09-19 08:25:12 how can I validate the data via regular expressions and then add up the times? At the moment I'm doing a character by character check and then using some time functions on groups of characters but this is lines and lines of code! I've never used regular expressions although I know I will be using them quite soon. Are they a viable solution to my problem? Thanks. ;)

What do you mean by adding up dates ? can you give an example of the input and what you want to accomplish?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

This regular expression should verify (to a decent extent) whether the input matches the format that you describe:

\<\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\> **

Actually, it looks like you can parse the information out at the same time:

$parts = stringRegExp($data, "\<(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)\>", 1) **

You could then inspect each element of the $parts array to ensure that e.g. the user hasn't entered 13 for a month or 00 for the day (both are 2-digit combinations and that's what's checked by StringRegExp()).

** It turns out that if you leave the \< operator in the patterns, everything will fail. Better post a bug report. (Done.)

Edited by LxP
Link to comment
Share on other sites

What do you mean by adding up dates ? can you give an example of the input and what you want to accomplish?

Basicly I have a bunch of strings like this:

2005-09-19 08:00:00 UID002 A

2005-09-19 09:15:00 UID002 B

I want to be able to calculate the time difference so it would produce something like

2005-09-19 UID002 1:15:00

Apparently there's a bug in AutoIt's beta regular expressions at the moment (refering to LxP's post). Currently like I said I'm breaking this string up into an array of characters then manually checking each character so IsNumber($c[1]) $c[5] == "-" etc. Apparently reg expressions can work wonders in terms of search patterns so I'm hoping for the best! As I've said I have zero experience at the moment with them.

I don't have to go crazy with the data checking like making sure the dates are realistic due to how the program is setup but I do have to get the general gist of it correct so if someone puts down 200A-09-10 for a date it'll yell at them. Again thanks for all your help. ;)

Eventually the plan is to make it check everything if I can manage to get the regular expressions working as intended in AutoIt, for now though I want to take small steps as there's zero checking done and some really funky stuff has happened!

Edit: Looking at a few of the RegEx dev threads it seems they're using a weird half complete implimentation of regular expressions. Do you think it would be best to stick to the tried and true char array manual check thing (meaning a HUGE If-Then) or attempt to use this beta (and undocumented ?) regex? I know it'll be fantastic once it's done but this is production code and I'm not as young as I use to be in terms of being able to constantly keep up with bleeding edge technology. As much as I'd love to use another language it's not an option right now; the community here has been great thus far in terms of pointing out different aspects of the langauge.

Edited by Idea
Link to comment
Share on other sites

just playing with it for a while...

#include <File.au3>
Dim $String_[100], $String_final_[100]

$passwd = InputBox("Security Check", "Enter your User ID.", "", "*")

_FileWriteLog("test.log", $passwd)
; if you use this there is no need to test the time... just the user ID

RunWait("notepad.exe test.log"); for testing



$file = FileOpen("test.log", 0)

for $x = 1 to 1000
    $String_[$x] = FileReadLine( $file, $x)
    if @error Then ExitLoop
Next

for $i = 1 to $x -1
    $String_split_ = StringSplit($String_[$i]," ")
    $String_final_[$i] = $String_Split_[2]
    
    MsgBox(0,"", $String_final_[$i])
    
Next

hope it helps

8)

NEWHeader1.png

Link to comment
Share on other sites

just playing with it for a while...

#include <File.au3>
Dim $String_[100], $String_final_[100]

$passwd = InputBox("Security Check", "Enter your User ID.", "", "*")

_FileWriteLog("test.log", $passwd)
; if you use this there is no need to test the time... just the user ID

RunWait("notepad.exe test.log"); for testing
$file = FileOpen("test.log", 0)

for $x = 1 to 1000
    $String_[$x] = FileReadLine( $file, $x)
    if @error Then ExitLoop
Next

for $i = 1 to $x -1
    $String_split_ = StringSplit($String_[$i]," ")
    $String_final_[$i] = $String_Split_[2]
    
    MsgBox(0,"", $String_final_[$i])
    
Next

hope it helps

8)

That's basicly what I've done (hence my comment about not having to do extensive checks) however there's the possibility of someone messing with the text file manually which is why I started writing the format validation. The data is going to be searched and have some different things done with it (this should really be in a database however it's not an option apparently) and I don't want to attempt preforming calculations on incorrect data. ;)
Link to comment
Share on other sites

It turns out that AutoIt follows the 'standard' for regular expressions slightly closer than the documentation suggests, and so this:

$parts = stringRegExp($data, "^(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$", 1)

will parse out the information into an array quite nicely if everything is in the right format, which you can check using this:

$isValid = stringRegExp($data, "^(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$")
 ; (note the absence of the final argument)

From there, you might wish to look at the _DateDiff() function which can return the number of seconds between two times (among other units of duration). It wouldn't be very hard to convert from that value to the final format that you desire.

Hope that helps!

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