ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > General Software Discussion

Regular Expressions (help)

(1/5) > >>

Contro:
 :-[

I need a little string conversion and I need the pattern with regular expressions I suppose.

This is the target

Date/Time format

01.02.2010.05.18   (where 01 is the day, 02 the month, 2010 the year, 05 hours, 18 minutes)

***CONTRO*** February 1, 2010 at 5:18am

In both cases I want the final string : 01_02_2010 , 5_18

How can I say this with a regular expression to use in the replace operation ?

Best Regards

 :-*

Renegade:
For the first one:

Match:
([0-9][0-9])\.([0-9][0-9])\.([0-9][0-9][0-9][0-9])\.([0-9][0-9])\.([0-9][0-9])

That's pretty specific/verbose, but is also very simple.

Replace:
\1_\2_\3 , \4_\5

But, that gives you 05 instead of 5. (I'm not feeling all that well, so maybe someone else can fix up a backreference and conditional there.)

Not sure about the second as for what everything could be, but you need a conditional for the month, and I'm a bit under the weather to remember and look it up right now. Maybe I can post back later.



Krishean:

--- Code: Javascript ---str = str.replace(/^(\d{2})\.(\d{2})\.(\d{4})\.0?(\d+)\.(\d{2})$/, "$1_$2_$3 , $4_$5");

4wd:
For the first one:

Match:
([0-9][0-9])\.([0-9][0-9])\.([0-9][0-9][0-9][0-9])\.([0-9][0-9])\.([0-9][0-9])

That's pretty specific/verbose, but is also very simple. -Renegade (January 06, 2013, 10:33 PM)
--- End quote ---

I went for simpler, (read: lazy :) )

Match: (..).(..).(....).(..).(..)

Also note depending on the renaming program you use, the backreference could be a $ instead of \

eg. For RegexRenamer, Renegades replace string would be:

Replace: $1_$2_$3 , $4_$5

Renegade:
Krishean has a good little option there. I'm rewriting what you have with his:

(..)\.(..)\.(....)\.0?(.+)\.(..)

You can repeat that pattern as needed:

0?(.+)

You do need the "\." though, otherwise you will match everything and a month from Jan~Sept will screw everything up on you.

Navigation

[0] Message Index

[#] Next page

Go to full version