seadoggie01 Posted March 6, 2019 Share Posted March 6, 2019 (edited) Disclaimer: I'm sure there is an easier way to do this without regex, but I like regex and the challenge. I have a date input in my GUI and I need to validate it and extract the year and month to enter them into another program. It accepts this format: YYYY-MM This is what I have currently: $DATE_PATTERN = "(?|([0-9]{4})[-\/\\]([0-9]{2})|([0-9]{2})[-\/\\]([0-9]{4}))" $Date = "2014/04" Msgbox($MB_OK, "Result", StringRegExpReplace($Date, $DATE_PATTERN, "\1-\2")) ;~ (Returns "2014-04") This works, but with 04/2014 it returns 04-2014 instead. This is what I want to work, but named groups don't seem to work like this... $DATE_PATTERN = "(?|(?<year>[0-9]{4})[-\/\\](?<month>[0-9]{2})|(?<month>[0-9]{2})[-\/\\](?<year>[0-9]{4}))" $Date = "2014/04" Msgbox($MB_OK, "Result", StringRegExpReplace($Date, $DATE_PATTERN, "\year-\month")) ;~ (Returns original code -- no match) I tried my code here: https://regex101.com/r/iJ9tC7/23 and it says that named groups can't be named that way. Is there a way to do this nicely in RegEx? I'm losing hope in it Edited March 6, 2019 by seadoggie01 Forgot to change replace pattern All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted March 6, 2019 Share Posted March 6, 2019 @seadoggie01 This is because backreferencing named capturing groups in substitutions, is not supported in PCRE engine Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
jchd Posted March 6, 2019 Share Posted March 6, 2019 A simple regexp can't do that this way. Not only patterns (named or not) only live while the regex engine scans the subject. PCRE (at least PCRE1 which is implemented in current AutoIt versions) has no support for substitution. The Replace part is an AutoIt construct which can only recognize $1, $2, $3, ... (or equivalently \1, \2, \3, ...) as a sugar for successive capturing groups. Due to the way regex works (try to match the pattern by scanning the subject left to right), you can't exchange the role of $1 and $2 depending on their content or order of matching. Yet there is a simple possibility: give up duplicate pattern numbering! Local $aDate = ["2014/04", "2014-04", "04-2014", "04\2014", "04/2014", "2014\04"] Local $DATE_PATTERN = "(\d{4})[-\/\\](\d{2})|(\d{2})[-\/\\](\d{4})" For $d In $aDate ConsoleWrite($d & " --> " & StringRegExpReplace($d, $DATE_PATTERN, "$1$4-$2$3") & @LF) Next Forcing capture of distinct fix-length subpatterns works, since what isn't matched yields an empty string, which we may concatenate transparently. pixelsearch, Dionysis, Fr33b0w and 1 other 3 1 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...
seadoggie01 Posted March 6, 2019 Author Share Posted March 6, 2019 @FrancescoDiMuro Oh. Go figure @jchd Dang! I remember thinking about something like that! (Probably tried and determined it wasn't possible) That's perfect!!!! Thank you! All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types 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