Jump to content

RegExp Named Groups


Recommended Posts

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 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 functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

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.

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)

Link to comment
Share on other sites

@FrancescoDiMuro Oh. Go figure :D

@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 functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

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