More importantly on how to format the date is the fact that you're not certain of the date format in the db. This should be your primary concern. Once you have that tackled, you can use whatever conversion you want: regex, substring & concatenate, texttodate, ...

All of the data entries should be yyyyMMdd. But it is very old system with several terabytes of data.

I like to keep a heads up for bad data entries. If I can work out a nice regEx pattern for dealing with getting the form correct yyyy-MM-dd, then I can rely on TextToDateValidate() to make sure 2000-02-29 does happen, but 2100-02-29 doesn't :-)

That doesn't seem a good application for regular expressions. Regular expressions are good for matching text. So if your regular expression matches the whole string, you will replace the whole string with the replacement character. So if you're sure about the format of the string, you'll probably be better of using Jorge's approach given that you don't want to replace text. You want to insert text into a string.

Now if you're just experimenting, a better example would be if your input text sometimes is formated as "2017/01/02" and sometimes "2017.01.02" and you want to format it like "2017-01-02". In that case you could use a regexp to match the "date separating character". That would be something like this "\/|\." to be replaced by "-".

The example I gave was a simplified version of my problem. Jorge's solution works. It can happily solve the simple problem that I have.

However, regEx is the better solution in the long term. The system I'm working on has a hell of a lot of legacy issues and knowing how to use regEx with Outsystems will make coding and maintenance easier.

As for this little test case - the dates in the tables I'm working with now all have the pattern 20160729 (yyyyMMdd). And they are all actually Strings. It's really old system. I'd bet dollars to doughnuts some of the data predates Thursday, 1 January 1970.

There is no delimiter between yyyy MM and dd. My understanding of TextToDate() is that it works with Strings that have a delimiter i.e. yyyy-MM-dd or yyyy/MM/dd or yyyy.MM.dd

So, I need to run 20160729 through (?=(?:\d{4}|\d{2})$)0? and then replace with -

To know how the syntax works, all I really need is a successful example of any regEx replace being used with OutSystems.

OutSystems uses the exact same regex syntax as .NET or Java does (whichever platform you are on). As far as I can tell, the Text extension simply passes your stuff to the relevant .NET or Java functions. If your regex works in those languages it *should* work in OutSystems.