mjolnirmarkiv Posted September 16, 2014 Share Posted September 16, 2014 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. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 16, 2014 Moderators Share Posted September 16, 2014 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) EndFuncYou 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
jchd Posted September 16, 2014 Share Posted September 16, 2014 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 hereRegExp tutorial: enough to get startedPCRE 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) Link to comment Share on other sites More sharing options...
Solution jguinch Posted September 16, 2014 Solution Share Posted September 16, 2014 (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 September 17, 2014 by jguinch Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
mjolnirmarkiv Posted September 17, 2014 Author Share Posted September 17, 2014 @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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now