Sign in to follow this  
Followers 0
mjolnirmarkiv

Yet another StringRegExp question

5 posts in this topic

Hi there!

I have two strings in two possible formats as seen below that I need to process and get a date.

I understand that it could be done with two patterns, but ideally I want to process it within single StringRegExp. As of now it returns following array of matches:

["August", "02", "2014"] for $s1, which is not exactly what I want

and

["2014", "08", "27"] for $s2, which is exactly what I want

I need to redo the pattern so it will return array of matches in following format ["YYYY", "MM", "DD"] for both strings.

#include <Array.au3>

$s1 = "Deadline Saturday, August 02, 2014 6:17 AM"
$s2 = "Deadline:    [2014-08-27 16:00:00 [Central European Time]]"
$pattern = "(?i)Deadline.*?(?:,\s|\[)(.*?)(?:\s|-)(.*?)(?:,\s|-)(.*?)\s"

_ArrayDisplay (StringRegExp ($s1, $pattern, 1))
_ArrayDisplay (StringRegExp ($s2, $pattern, 1))

Thank in advance for help!

 

P.S.: converting name of the month into a double-digit code is a problem for another time, shouldn't be too hard.

Share this post


Link to post
Share on other sites



mjolnirmarkiv,

No doubt one of the RegEx gurus will be along at some point to show you how it can be done in one pass, but I would do it like this (and you get the month converted to numeric form as well ;)):

#include <Array.au3>
#include <StringConstants.au3>
#include <DTC.au3>

$s1 = "Deadline Saturday, August 02, 2014 6:17 AM"
$s2 = "Deadline:    [2014-08-27 16:00:00 [Central European Time]]"

$aRet = _ReadDate($s1)
_ArrayDisplay($aRet, "", Default, 8)

$aRet = _ReadDate($s2)
_ArrayDisplay($aRet, "", Default, 8)

Func _ReadDate($sString)

    ; Check which type of string is passed
    If StringRegExp($sString, "\d{4}-\d{2}-\d{2}") Then
        ; Extract date elements
        $sDate = StringRegExpReplace($sString, ".*\[(\d{4})-(\d{2})-(\d{2}).*", "$1 $2 $3")
    Else
        ; Extract date elements
        $sDate = StringRegExpReplace($sString, ".*,\s(\w+)\s(\d{2}),\s(\d{4}).*", "$2 $1 $3")
        ; Convert to numeric month
        $sDate = _Date_Time_Convert($sDate, "dd MMMM yyyy", "yyyy MM dd")
    EndIf
    ; Split string into array
    Return StringSplit($sDate, " ", $STR_NOCOUNT)

EndFunc
You can find the link to my DateTimeConvert UDF in my sig. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

No better idea here. "When NOT to use XYZ" is the point.


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

#4 ·  Posted (edited)

There is certainly easier, but here is a way :

#include <Array.au3>

$s1 = "Deadline Saturday, August 02, 2014 6:17 AM"
$s2 = "Deadline:    [2014-08-27 16:00:00 [Central European Time]]"

$pattern = "(?xmi)^Deadline[:\h]+(?|  (?=(?:\[(\d{4})-(\d{2})-(\d{2}))) |  (?:(?=[a-z]+,\h+.*?(\d{4})) (?=.*?,\h+(\w+))  (?=.*?(\d{2}))  ) )"

_ArrayDisplay( StringRegExp($s1, $pattern, 3) )
_ArrayDisplay( StringRegExp($s2, $pattern, 3) )

http://regex101.com/r/pN3hG2/3

Edited by jguinch

Share this post


Link to post
Share on other sites

@Melba23

Well aware of that UDF, thanks!

@jguinch

Much appreciated, I knew look ahead is the key here, but didn't think of using capturing groups inside it :)

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
Sign in to follow this  
Followers 0