Syntax for Regex Replace

Syntax for Regex Replace



I'm to convert a String "20160727" to "2016-07-27"

The regex pattern I'm using is 

"(?=(?:\d{4}|\d{2})$)0? gm"

and the replace is


The pattern works

But I'm unsure of the syntax for entering into the Regex_Replace execute action.

I know TextToDate() might be easier, but I cannot be certain the String from the DB query will always be yyyyMMdd. So I need to build a regex pattern to handle date formats errors.

Thank you.

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

Hello Kurt,

Thank you for your interest in my problem.

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 :-)

Try this


with assigns:

year = substr(text,0,4)



finaldate= year + "-" + month + "-" + day

where year, month, day and finaldate are local variables of text type (string).

Does it help?

Hello Jorge,

Thank you for your interest in my problem.

Yes, that solution works.

Though my goal is to figure out how to use regEx in Outsystems. Just putting the pattern and the replace in quotation marks doesn't seem to work.

RegEx is a wonderful tool. Like a surgeon's scalpel - can save one from cancer, but just as easily slice your finger off.

Hi Mark!

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

Does this make sense?

Hello Rui,

Thank you for your interest in my problem.

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. 


"(?=(?:\d{4}|\d{2})$)0? gm"

is not working in eSpace.

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.