Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 02, 2016, 08:31:57 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Regular Expressions (help)  (Read 4194 times)

Contro

  • Participant
  • Joined in 2007
  • *
  • Posts: 2,619
    • View Profile
    • Donate to Member
Regular Expressions (help)
« on: January 06, 2013, 10:25:47 PM »
 :-[

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

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,220
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: Regular Expressions (help)
« Reply #1 on: January 06, 2013, 10:33:44 PM »
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.



Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Krishean

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 75
  • I like pie
    • View Profile
    • Draconis Labs
    • Donate to Member
Re: Regular Expressions (help)
« Reply #2 on: January 06, 2013, 10:43:04 PM »
Code: Javascript [Select]
  1. str = str.replace(/^(\d{2})\.(\d{2})\.(\d{4})\.0?(\d+)\.(\d{2})$/, "$1_$2_$3 , $4_$5");
Any sufficiently advanced technology is indistinguishable from magic.

- Arthur C. Clarke

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,471
    • View Profile
    • Donate to Member
Re: Regular Expressions (help)
« Reply #3 on: January 06, 2013, 10:44:23 PM »
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.

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

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,220
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: Regular Expressions (help)
« Reply #4 on: January 06, 2013, 10:55:21 PM »
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.

Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,471
    • View Profile
    • Donate to Member
Re: Regular Expressions (help)
« Reply #5 on: January 06, 2013, 11:00:05 PM »
(read: lazy :) )

I'm waiting to see the RegEx for the second instance....if it's possible.

Krishean

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 75
  • I like pie
    • View Profile
    • Draconis Labs
    • Donate to Member
Re: Regular Expressions (help)
« Reply #6 on: January 06, 2013, 11:14:34 PM »
It's not possible to use a RegExp to transform month names into numeric values, and the 12h date format manipulation is also outside of RegExp's capabilities. It is possible to match those strings and use additional logic outside of RegExp to process them.
Any sufficiently advanced technology is indistinguishable from magic.

- Arthur C. Clarke

Contro

  • Participant
  • Joined in 2007
  • *
  • Posts: 2,619
    • View Profile
    • Donate to Member
Re: Regular Expressions (help)
« Reply #7 on: January 06, 2013, 11:35:54 PM »
 :-[

What I need is convert those string in the clipboard when I paste with the special hotkey control+alt+win+2

A conversion string tool.

A code snack ?

 ;D

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,220
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: Regular Expressions (help)
« Reply #8 on: January 06, 2013, 11:37:41 PM »
It's not possible to use a RegExp to transform month names into numeric values, and the 12h date format manipulation is also outside of RegExp's capabilities. It is possible to match those strings and use additional logic outside of RegExp to process them.

I've been doing some reading, and from what I can tell, regular expression conditionals are only for matching. So, unfortunately Krishean is right. :(

Guess that explains why I've never used conditional replacements before. :( It's always just been easier and simpler to do something like .Replace("something", "another thing").

Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,471
    • View Profile
    • Donate to Member
Re: Regular Expressions (help)
« Reply #9 on: January 06, 2013, 11:50:30 PM »
What I need is convert those string in the clipboard when I paste with the special hotkey control+alt+win+2

A conversion string tool.

A code snack ?

Clipboard Help + Spell ?

Contro

  • Participant
  • Joined in 2007
  • *
  • Posts: 2,619
    • View Profile
    • Donate to Member
Re: Regular Expressions (help)
« Reply #10 on: January 07, 2013, 07:49:37 AM »
 :-*

Running to try.
Do you have an aproximate idea what I have to do ?
 ;D

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,220
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: Regular Expressions (help)
« Reply #11 on: January 07, 2013, 07:56:43 AM »
I'm waiting to see the RegEx for the second instance....if it's possible.

Second regex:

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

[^0-9]+ ([0-9]+), ([0-9]+) at ([0-9]+):([0-9]+)(am|pm)

Those are:

1 = 1
2 = 2010
3 = 5
4 = 18
5 = am

***CONTRO*** February isn't captured as you need programmatic logic to get the month there, which is a simple replacement.
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Contro

  • Participant
  • Joined in 2007
  • *
  • Posts: 2,619
    • View Profile
    • Donate to Member
Re: Regular Expressions (help)
« Reply #12 on: January 07, 2013, 01:05:44 PM »
 :-[

Difficult indeed

Don't exist a script for this purpose ?

Best Regards
 :-*

robinsiebler

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 83
    • View Profile
    • Donate to Member
Re: Regular Expressions (help)
« Reply #13 on: January 08, 2013, 01:55:32 PM »
There are a slew of RE tester/tutor programs on the 'net. I use Expresso
Happiness is laced with shards of pain

Contro

  • Participant
  • Joined in 2007
  • *
  • Posts: 2,619
    • View Profile
    • Donate to Member
Re: Regular Expressions (help)
« Reply #14 on: January 08, 2013, 02:32:12 PM »
There are a slew of RE tester/tutor programs on the 'net. I use Expresso

Owwww. But it's not possible in my case. We keep trying
Best Regards
 :P

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,220
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: Regular Expressions (help)
« Reply #15 on: January 08, 2013, 07:31:52 PM »
You need to be explicit about where and how you need to use this. Expresso is an excellent program, and would certainly let you get the job done.
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Contro

  • Participant
  • Joined in 2007
  • *
  • Posts: 2,619
    • View Profile
    • Donate to Member
Re: Regular Expressions (help)
« Reply #16 on: January 08, 2013, 10:09:51 PM »
You need to be explicit about where and how you need to use this. Expresso is an excellent program, and would certainly let you get the job done.

Me or Robin ?
 :-[

robinsiebler

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 83
    • View Profile
    • Donate to Member
Re: Regular Expressions (help)
« Reply #17 on: January 09, 2013, 03:14:11 PM »
You, silly! I was quite specific. Why won't that program, or any of the others like it, work for you?
Happiness is laced with shards of pain

Contro

  • Participant
  • Joined in 2007
  • *
  • Posts: 2,619
    • View Profile
    • Donate to Member
Re: Regular Expressions (help)
« Reply #18 on: January 09, 2013, 03:58:52 PM »
 :-\

I keep trying then.
 :-*

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,471
    • View Profile
    • Donate to Member
Re: Regular Expressions (help)
« Reply #19 on: January 13, 2013, 04:58:59 AM »
Code: AutoIt [Select]
  1. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  2. #AutoIt3Wrapper_UseUpx=n
  3. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  4.  
  5. ;$inclip = "***CONTRO*** February 28, 2010 at 5:18am"
  6. ;$inclip = '01.02.2010.05.18'
  7.  
  8. HotKeySet('^#!2', '_Doit') ; Win+Alt+Ctrl+2
  9.  
  10.         Sleep(1000)
  11.  
  12.  
  13.  
  14. Func _Doit()
  15.         $inclip = ClipGet()
  16.  
  17.         If StringLeft($inclip, 12) <> '***CONTRO***' Then
  18.                 $outclip = StringRegExpReplace($inclip, '(..)\.(..)\.(....)\.0?(.+)\.(..)', '\1_\2_\3 , \4_\5')
  19.         Else
  20.                 $outclip = _Valmorphanize(StringMid($inclip, 14))
  21.         EndIf
  22.  
  23.         ClipPut($outclip)
  24.         Sleep(50)
  25.         Send('^v')
  26.  
  27. Func _Valmorphanize($temp)
  28.         Local $hour = StringLeft(StringRight($temp, 7), StringInStr(StringRight($temp, 7), ':') - 1) + (12 * (StringRight(StringRight($temp, 7), 2) = 'pm'))
  29.         If Not Mod($hour, 12) Then $hour-=12
  30.         Return StringRegExpReplace($temp, '.+\b.\d+, (\d{4}).+:(\d{2}).*', StringFormat('%02s', StringRegExpReplace(StringMid($temp, StringInStr($temp, ' ') + 1, 2), '([,\s])', '')) & '_' & StringFormat('%02s', Int(StringInStr('JanFebMarAprMayJunJulAugSepOctNovDec', StringLeft($temp, 3), 1) / 3) + 1) & '_\1 , ' & $hour & '_\2')

Contro

  • Participant
  • Joined in 2007
  • *
  • Posts: 2,619
    • View Profile
    • Donate to Member
Re: Regular Expressions (help)
« Reply #20 on: January 13, 2013, 09:12:12 AM »
Code: AutoIt [Select]
  1. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  2. #AutoIt3Wrapper_UseUpx=n
  3. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  4.  
  5. ;$inclip = "***CONTRO*** February 28, 2010 at 5:18am"
  6. ;$inclip = '01.02.2010.05.18'
  7.  
  8. HotKeySet('^#!2', '_Doit') ; Win+Alt+Ctrl+2
  9.  
  10.         Sleep(1000)
  11.  
  12.  
  13.  
  14. Func _Doit()
  15.         $inclip = ClipGet()
  16.  
  17.         If StringLeft($inclip, 12) <> '***CONTRO***' Then
  18.                 $outclip = StringRegExpReplace($inclip, '(..)\.(..)\.(....)\.0?(.+)\.(..)', '\1_\2_\3 , \4_\5')
  19.         Else
  20.                 $outclip = _Valmorphanize(StringMid($inclip, 14))
  21.         EndIf
  22.  
  23.         ClipPut($outclip)
  24.         Sleep(50)
  25.         Send('^v')
  26.  
  27. Func _Valmorphanize($temp)
  28.         Local $hour = StringLeft(StringRight($temp, 7), StringInStr(StringRight($temp, 7), ':') - 1) + (12 * (StringRight(StringRight($temp, 7), 2) = 'pm'))
  29.         If Not Mod($hour, 12) Then $hour-=12
  30.         Return StringRegExpReplace($temp, '.+\b.\d+, (\d{4}).+:(\d{2}).*', StringFormat('%02s', StringRegExpReplace(StringMid($temp, StringInStr($temp, ' ') + 1, 2), '([,\s])', '')) & '_' & StringFormat('%02s', Int(StringInStr('JanFebMarAprMayJunJulAugSepOctNovDec', StringLeft($temp, 3), 1) / 3) + 1) & '_\1 , ' & $hour & '_\2')

Running to tryyyyy
 :-*