Chiitus

Regex date

16 posts in this topic

#1 ·  Posted (edited)

Hello guys, i have a problem in this regex because its not groupping right.

Look:

(\d{1,2}\s?[\055|\056|\057|\134]\s?\d{1,2}(\s?[\055|\056|\057|\134]\s?\d{2,4})?)

It checks for day or dayday, month or monthmonth, yearyear or yearyearyear+year.

separated with or without space and the characters "\/.-"

But, a problem! I tried to regex the date with or without the "separator + year" Eg: 05/05 or 05/05/05, but when i have 05/05/05, regex are separating 05/05 and /05

Iam using group "()" wrong?

Need help here :D

Thx adv!

~~EDIT:

Script used: (from autoitscript.com/forum/topic/129697-split-string-by-regular-expression/#comment-901335)

#include <Array.au3>

Local $sString = ClipGet()

; Extract the delimiters
$aDelim = StringRegExp($sString, '(\d{1,2}\s?[\055|\056|\057|\134]\s?\d{1,2}(\s?[\055|\056|\057|\134]\s?\d{2,4})?)', 3)

; Split the string on the delimiters
$aParts = StringSplit(StringRegExpReplace($sString, '(\d{1,2}\s?[\055|\056|\057|\134]\s?\d{1,2}(\s?[\055|\056|\057|\134]\s?\d{2,4})?)', "$<SEPARATOR>$"), "$<SEPARATOR>$", 3)

; Add the delimter to the start of the part
For $i = 0 To UBound($aParts) - 1
   ;$aParts[$i + 1] = $aDelim[$i] & $aParts[$i + 1]
Next

; Set the count
$aParts[0] = UBound($aParts)

; Display the result
_ArrayDisplay($aParts)
_ArrayDisplay($aDelim)

Results in array: (the array parts in all string is ok; but the string delimitator himself is getting separated in every string that have year together)

temp.png

String used as test:

Quote

02 / 9 \ 16 – test

07 - 9 . 16 – test

09/9/16 – test

14\9\16 – test

16-9-16 – test


21.9.16 – test


23/9 – test


28\9 – test

Cheers.

Edited by Chiitus

Share this post


Link to post
Share on other sites



2 hours ago, Jos said:

It helps when you post a small script that demonstrates the issue, so we can have a play with it.

Jos

Topic edited!

Thanks for the info :D

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

3 hours ago, Chiitus said:

Local $sString = ClipGet()

What about the string to test with?

Ok copied your test data into a file to make life easy.

Jos

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

12 minutes ago, jguinch said:

are you sure that the pattern [\055|\056|\057|\134] matches what you expect ? https://regex101.com/r/zQ5xY4/1

 

It match "\/.-" one of these chars. But in this link you posted, regex are founding char "|", isnt this char a "OR" operator in regex?

EDIT: From AutoIt regex tutorial link: codeproject.com/Articles/9099/The-Minute-Regex-Tutorial;

\xxx ~> Character ASCII octal code.

Edited by Chiitus

Share this post


Link to post
Share on other sites

I find it rare that people use octal nowadays, in regexp patterns or elsewhere.

1 person likes this

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

Inside a character class, the bar | doesn't mean alternation, it stands for itself (redondantly here).


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

But one question remains: what format do you want the final result to be?


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

If you do not want the delimliter and year only to be specifically matched then make that part of the RE pattern a non-capturing group.
See example.

#include <Array.au3>

Local $sString = StringRegExpReplace(FileRead(@ScriptFullPath), "(?is)^.*#cs\R|\R?#ce.*$", "") ; ClipGet()
ConsoleWrite($sString & @CRLF)

$a = StringRegExp($sString, "((?:[0-9]{1,2}[\h\-\\/.]{0,3}){2,3})", 3)
_ArrayDisplay($a)

; Modified $aDelim from Post#1
$aDelim = StringRegExp($sString, '(\d{1,2}\s?[\055|\056|\057|\134]\s?\d{1,2}(?:\s?[\055|\056|\057|\134]\s?\d{2,4})?)', 3)
; Note addition of "?:" in the RegExp pattern in the line above here~~~~~~~~~^
_ArrayDisplay($aDelim, "$aDelim")

; Data for variable $sString - Copied from Post#1
#cs

02 / 9 \ 16 – test

07 - 9 . 16 – test

09/9/16 – test

14\9\16 – test

16-9-16 – test


21.9.16 – test


23/9 – test


28\9 – test

#ce

 

1 person likes this

Share this post


Link to post
Share on other sites
23 hours ago, jchd said:

But one question remains: what format do you want the final result to be?

Ya, octal was stupid move, i changed to char himself. Thx!

Share this post


Link to post
Share on other sites
15 hours ago, Malkey said:

If you do not want the delimliter and year only to be specifically matched then make that part of the RE pattern a non-capturing group.
See example.

#include <Array.au3>

Local $sString = StringRegExpReplace(FileRead(@ScriptFullPath), "(?is)^.*#cs\R|\R?#ce.*$", "") ; ClipGet()
ConsoleWrite($sString & @CRLF)

$a = StringRegExp($sString, "((?:[0-9]{1,2}[\h\-\\/.]{0,3}){2,3})", 3)
_ArrayDisplay($a)

; Modified $aDelim from Post#1
$aDelim = StringRegExp($sString, '(\d{1,2}\s?[\055|\056|\057|\134]\s?\d{1,2}(?:\s?[\055|\056|\057|\134]\s?\d{2,4})?)', 3)
; Note addition of "?:" in the RegExp pattern in the line above here~~~~~~~~~^
_ArrayDisplay($aDelim, "$aDelim")

; Data for variable $sString - Copied from Post#1
#cs

02 / 9 \ 16 – test

07 - 9 . 16 – test

09/9/16 – test

14\9\16 – test

16-9-16 – test


21.9.16 – test


23/9 – test


28\9 – test

#ce

 

Worked like a charm!!

Thank you!!!

Share this post


Link to post
Share on other sites
9 hours ago, pluto41 said:

This works also. Sort of.

#include <array.au3>
Local $date = "01.12\1967"  ; test data

Local $a = StringRegExp ( $date, "\d{1,2}\s?[-/\.\\]\s?\d{1,2}\s?[-/\.\\]\s?\d{2,4}", 3)
_ArrayDisplay($a)

 

Not testes, but thanks anyway!!

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

How can i mark as solved this topic?

Amazing forum guys, thanks for the help!!!

 

~~EDIT:

I did a improve in the regex code.

Now check for a valid day (0-31; dont check by month) and check a valid month (1-12).

Including leading zero.

Check it out:

$aDelim = StringRegExp($texto, '((?:\n0[1-9]\s?[|\\/.-]\s?\b(?:0[1-9]|[1-9]|1[1-2])\b|\n[1-9]\s?[|\\/.-]\s?\b(?:0[1-9]|[1-9]|1[1-2])\b|\n[1-2][0-9]\s?[|\\/.-]\s?\b(?:0[1-9]|[1-9]|1[1-2])\b|\n3[0-1]\s?[|\\/.-]\s?\b(?:0[1-9]|[1-9]|1[1-2])\b)(?:\s?[|\\/.-]\s?\d{2,4})?)', 3)

 

Edited by Chiitus

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

    • MyEarth
      By 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
      Sorry if i don't provide a working code, regex is too way complex.
      Thanks
    • 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!
    • ur
      By ur
      To get the current time stamp, I got the below code.
      #include <Date.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> #RequireAdmin ; Under Vista the Windows API "SetSystemTime" may be rejected due to system security $td = _Date_Time_GetSystemTime() $td = _Date_Time_SystemTimeToDateTimeStr($td) $td = StringReplace($td, " ", "_") $td = StringReplace($td, ":", "_") MsgBox(0,"",$td) But it is not giving the date or time of the timezone where the system is there.
      Please suggestt
    • Trong
      By Trong
      Hi,
      I need help string RegEx to get string from CREATE to GO
      #include <StringConstants.au3> ;~ Global $fileSQL1 = @ScriptDir & "\fileSQL1.sql" ;~ Global $fileSQL2 = @ScriptDir & "\fileSQL2.sql" Global $tmpSQLfile = @TempDir & "\tmpFile.sql" OnAutoItExitRegister("_OnExit") _SetTMPsql() If Not FileExists($tmpSQLfile) Then OnAutoItExitUnRegister("_OnExit") Exit MsgBox(48, "/!\", "File: " & $tmpSQLfile & @CRLF & " is not Exists!", 3) EndIf Global $ContentSQLfile = FileRead($tmpSQLfile) _Start() Func _Start() Local $aArray, $iOffset = 1, $stringRegExp = '(?i)CREATE(.*?)GO' While 1 $aArray = StringRegExp($ContentSQLfile, $stringRegExp, $STR_REGEXPARRAYMATCH, $iOffset) If @error Then MsgBox(48, "StringRegExp Error " & @error, "+> StringRegExp: " & $stringRegExp & @CRLF & @CRLF & "=> With STRING:" & @CRLF & @CRLF & $ContentSQLfile) ExitLoop EndIf $iOffset = @extended For $i = 0 To UBound($aArray) - 1 MsgBox(0, "RegExp Test with Option 1 - " & $i, $aArray[$i]) Next WEnd EndFunc ;==>_Start Func _SetTMPsql() Local $tmpSQLContent = "" $tmpSQLContent &= "USE [Master]" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "CREATE DATABASE [Sales] ON PRIMARY " & @CRLF $tmpSQLContent &= "( NAME = N’Sales’, FILENAME = N’\FSASQLDBSales.mdf’ , " & @CRLF $tmpSQLContent &= " SIZE = 2GB , MAXSIZE = 8GB, FILEGROWTH = 1GB )" & @CRLF $tmpSQLContent &= "LOG ON " & @CRLF $tmpSQLContent &= "( NAME = N’Sales_log’, FILENAME = N’\FSASQLDBSales_log.ldf’ , " & @CRLF $tmpSQLContent &= " SIZE = 1GB , MAXSIZE = 2GB , FILEGROWTH = 10%)" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "USE [Sales]" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "-- Table Product" & @CRLF $tmpSQLContent &= "CREATE TABLE [dbo].[Product]" & @CRLF $tmpSQLContent &= "(" & @CRLF $tmpSQLContent &= " [ProductId] [uniqueidentifier] DEFAULT NEWID() NOT NULL," & @CRLF $tmpSQLContent &= " [ProductName] [nchar](50) NULL," & @CRLF $tmpSQLContent &= " [ProductDescription] [nchar](3000) NULL," & @CRLF $tmpSQLContent &= " [ProductPrice] MONEY NULL" & @CRLF $tmpSQLContent &= ") ON [PRIMARY]" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "-- Table Sales" & @CRLF $tmpSQLContent &= "CREATE TABLE [dbo].[Sales]" & @CRLF $tmpSQLContent &= "( " & @CRLF $tmpSQLContent &= " [SaleId] [uniqueidentifier] DEFAULT NEWID() NOT NULL," & @CRLF $tmpSQLContent &= " [SaleName] [nchar](50) NULL," & @CRLF $tmpSQLContent &= " [SaleInfo] [nchar](3000) NULL," & @CRLF $tmpSQLContent &= " [SaleMoney] MONEY NULL" & @CRLF $tmpSQLContent &= ") ON [PRIMARY]" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "SET ANSI_NULLS ON" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "SET QUOTED_IDENTIFIER ON" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "-- The End" & @CRLF Local $hOpen = FileOpen($tmpSQLfile, 2 + 8 + 128) FileWrite($hOpen, $tmpSQLContent) Return FileClose($hOpen) EndFunc ;==>_SetTMPsql Func _OnExit() Exit FileDelete($tmpSQLfile) EndFunc ;==>_OnExit  
      mikell