Page 1 of 1

Noob requires help with regex and back references

Posted: Fri Mar 26, 2010 10:10 am
by mr2magic
Hello everyone,

I'm completely new to regexp so bear with me if my problem turns out to be boringly simple.

I have a date - 1.1.2010
this date needs to be converted to a format whereby all single digit numbers are preceded by 0 giving me 01.01.2010.

My regexp expression looks like this (it's our own script language so you may not know the syntax)
REGEX_REPLACE('(\d{1})(\.)(\d{1})(\.)(\d+)','1.1.2010','0$1$20$3$4$5')

The problem area is highlighted red. If I try to add a zero directly after the reference number 2 regex interprets this as reference number 20 and returns a null string :( How on earth do I complete my string?

Any help is appreciated.

Greets from Germany!

Re: Noob requires help with regex and back references

Posted: Fri Mar 26, 2010 10:41 am
by AbraCadaver
I'm not sure in your own scripting language, but assuming you always want dots why not adjust the pattern and the replace like so:

Code: Select all

REGEX_REPLACE('(\d{1})\.(\d{1})\.(\d+)', '1.1.2010', '0$1.0$2.$3')

Re: Noob requires help with regex and back references

Posted: Fri Mar 26, 2010 11:17 am
by mr2magic
Well smoke me a kipper! I'll be back for breakfast 8O :D Thank you for that!

I did think of that but it was to easy 8)

Does that then mean I beat Regex because there is no other way to do this?

edit: Just tested that by the way. Now I need to try to get it to work for all combinations

01.1.2010
1.01.2010
1.1.2010

Is there any way to do this

Re: Noob requires help with regex and back references

Posted: Fri Mar 26, 2010 4:20 pm
by ridgerunner
I don't think you can do it with just one regex. You'll need two of them minimum. Here is a solution in PHP preg_replace format:

Code: Select all

// replace single digit in first position (second position can have one or two)
$contents = preg_replace('/^(\d)\.(\d\d?)\.(\d{4})$/', '0$1.$2.$3', $contents);
// replace single digit in second position (first position must have two)
$contents = preg_replace('/^(\d\d)\.(\d)\.(\d{4})$/', '$1.0$2.$3', $contents);
Note that you need the "beginning of string" anchor metacharacter '^' for the first pattern above to avoid turning something like this: '11.2.2010' into this: '101.2.2010'.

p.s. love the "Ace" red dwarf reference! :)

Re: Noob requires help with regex and back references

Posted: Mon Mar 29, 2010 4:05 am
by mr2magic
Ok, back from a weekend doing anything but thinking about regular expressions :D

Thanks for the hint. Out scripting language is not procedural so loops etc. are not possible (it's more like SQL than anything really).


I solved it like this.

REGEX_REPLACE('(\d\d)\.(\d)\.(\d+)',REGEX_REPLACE('^(\d{1})\.(\d\d?)\.(\d+)','1.01.2010','0$1.$2.$3'),'$1.0$2.$3')

Yeah! Red dwarf FTW :o) Have all episodes on HDD!