regex help on prepending leading zeroes

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
tiago
Posts: 589
Joined: 14 Feb 2011 21:41

regex help on prepending leading zeroes

Post by tiago »

Hi.

I guess regexreplace is the way to go so how do I get 'hh:nn d/mm/yyyy' entries replaced by 'hh:nn dd/mm/yyyy' so '15:35 1/10/2012' turns '15:35 01/10/2012'? On the other hand '07:34 25/05/2011' must be ignored as 'day' has two digits already.

Thanks.
Power-hungry user!!!

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: regex help on prepending leading zeroes

Post by Stefan »

tiago wrote:Hi.

I guess regexreplace is the way to go so how do I get

'hh:nn d/mm/yyyy' entries replaced by
'hh:nn dd/mm/yyyy'
so
'15:35 1/10/2012' turns
'15:35 01/10/2012'?

On the other hand '07:34 25/05/2011' must be ignored as 'day' has two digits already.

Thanks.
From where do you get this timestamps? They can be proper formated already in first place.

If you can do anything to the input format, then:
where do you need to modify this timestamps?
Are that just strings in an text file?
Didn't formatdate() come to aid here?

formatdate([date], [format]);
::text formatdate('15:35 1/10/2012', 'hh:mm dd/mm/yyyy');



.

FluxTorpedoe
Posts: 906
Joined: 05 Oct 2011 13:15

Re: regex help on prepending leading zeroes

Post by FluxTorpedoe »

Hi'
In case for some reason you can't use a cleaner approach like Stefan's solution, and you still need to use some regex, here's a quick and very basic answer (though not foolproof if your date is surrounded by other similar data):

Code: Select all

  $mydate="15:35 1/10/2012";
  echo regexreplace($mydate,"\s(\d)/"," 0$1/");
(Of course you can enter your date directly into the regex.)
It works by adding a zero before any single "digit" [(\d)] (parenthesis later referenced by [$1]), which is surrounded by a "space" [\s] and a "/".

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: regex help on prepending leading zeroes

Post by Stefan »

What's about
$mydate="15:35 11/1/2012";
$mydate="15:35 1/1/2012";
$mydate="5:35 1/10/2012";

FluxTorpedoe
Posts: 906
Joined: 05 Oct 2011 13:15

Re: regex help on prepending leading zeroes

Post by FluxTorpedoe »

Hi all,
& Stefan BTW, thanks for your scripting tips which where one of my first reads after I got XY :biggrin:

So to sum things up:
1. Stefan's right in drawing the attention to the misuse of regexes. They shouldn't be used as a first resort!
2. To answer the OP's topic "regex help on prepending leading zeroes" before 'one digit' days: the code works anyway :wink: - even with the above examples. It just doesn't do anything more than what it was meant to do...
-- that is, it won't prepend zeros for the months, hours, or change 24h into AM/PM, etc. It could be done one convoluted way or the other, but then we're back to square 1.:
:arrow: If you want to mangle a $tring, you could go with regex - but as a second choice. First, it's always best to check if there's no better solution (e.g. in the case of dates, the formatdate as suggested above).
Cheers 8)

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: regex help on prepending leading zeroes

Post by tiago »

My preference goes for regexreplace as it does a single search-and-replace operation on a whole string, no matter how big it is.

So generally I agree formatdate is the way to go when it comes to precision but in that specific case the day was the problematic token.

Of course if someone can provide a full-fledged regular expression it would be very useful for the other cases I have, ie, no token should be allowed to have a single digit (4 - not ok, 04 - ok!) as the expected format is 'hh:nn dd/mm/yyyy'.

Thanks, thanks!
Power-hungry user!!!

FluxTorpedoe
Posts: 906
Joined: 05 Oct 2011 13:15

Re: regex help on prepending leading zeroes

Post by FluxTorpedoe »

Hi'
no token should be allowed to have a single digit
Shoot, Maverick!

Code: Select all

  $mydate="<crlf>5:35 1/10/2012<crlf>15:35 11/1/2012<crlf>15:35 1/1/2012<crlf>5:35 1/10/2012<crlf>5:35 1/1/2012";
  echo $mydate;
  echo regexreplace($mydate,"(\D)(\d)(?=(:|/))","$10$2");
Note 1. The first line is just a combination of all the above date examples so the regex can be checked quickly with before/after.
Note 2. It's not focused on "single digits" only (because you could have other single digits elsewhere in your string), but it identifies any isolated digit, preceded by "no digit" and followed by ":" or "/", and then adds a zero before them.
Note 3. Since it's implemented to find a digit preceded by no digit, it won't have any effect on the very first character of your string (which is preceded by nothing). So if you're really desperate to start your string with a single digit, think again, be kind with me, and add a line-feed (or whatever) before it - or wait for a cleaner solution. :mrgreen:

Anyway as was already said, regexes aren't all-around solutions. Without knowing the context, it's hard to create a single adaptable regex.
So I hope this one can do what you expected...

Enjoy! 8)
Flux

Post Reply