Noob requires help with regex and back references

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mr2magic
Forum Newbie
Posts: 3
Joined: Fri Mar 26, 2010 10:03 am

Noob requires help with regex and back references

Post 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!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Noob requires help with regex and back references

Post 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')
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mr2magic
Forum Newbie
Posts: 3
Joined: Fri Mar 26, 2010 10:03 am

Re: Noob requires help with regex and back references

Post 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
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Noob requires help with regex and back references

Post 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! :)
mr2magic
Forum Newbie
Posts: 3
Joined: Fri Mar 26, 2010 10:03 am

Re: Noob requires help with regex and back references

Post 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!
Post Reply