DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Regular Expressions (help)  (Read 15,553 times)

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Regular Expressions (help)
« on: January 07, 2013, 04:25 AM »
:-[

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

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Regular Expressions (help)
« Reply #1 on: January 07, 2013, 04:33 AM »
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

  • I like pie
  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 75
    • Participated in NANY Challenge Three Times
Re: Regular Expressions (help)
« Reply #2 on: January 07, 2013, 04:43 AM »
Code: (javascript)
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

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Regular Expressions (help)
« Reply #3 on: January 07, 2013, 04:44 AM »
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
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Regular Expressions (help)
« Reply #4 on: January 07, 2013, 04:55 AM »
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

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Regular Expressions (help)
« Reply #5 on: January 07, 2013, 05:00 AM »
(read: lazy :) )

I'm waiting to see the RegEx for the second instance....if it's possible.
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

Krishean

  • I like pie
  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 75
    • Participated in NANY Challenge Three Times
Re: Regular Expressions (help)
« Reply #6 on: January 07, 2013, 05:14 AM »
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

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Re: Regular Expressions (help)
« Reply #7 on: January 07, 2013, 05:35 AM »
:-[

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

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Regular Expressions (help)
« Reply #8 on: January 07, 2013, 05:37 AM »
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

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Regular Expressions (help)
« Reply #9 on: January 07, 2013, 05:50 AM »
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 ?
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Re: Regular Expressions (help)
« Reply #10 on: January 07, 2013, 01:49 PM »
:-*

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

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Regular Expressions (help)
« Reply #11 on: January 07, 2013, 01:56 PM »
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

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Re: Regular Expressions (help)
« Reply #12 on: January 07, 2013, 07:05 PM »
:-[

Difficult indeed

Don't exist a script for this purpose ?

Best Regards
:-*

robinsiebler

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 83
    • Military Service Badge - US Marine Corp
Re: Regular Expressions (help)
« Reply #13 on: January 08, 2013, 07:55 PM »
There are a slew of RE tester/tutor programs on the 'net. I use Expresso
Happiness is laced with shards of pain

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Re: Regular Expressions (help)
« Reply #14 on: January 08, 2013, 08:32 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

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Regular Expressions (help)
« Reply #15 on: January 09, 2013, 01:31 AM »
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

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Re: Regular Expressions (help)
« Reply #16 on: January 09, 2013, 04:09 AM »
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
    • Military Service Badge - US Marine Corp
Re: Regular Expressions (help)
« Reply #17 on: January 09, 2013, 09:14 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

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Re: Regular Expressions (help)
« Reply #18 on: January 09, 2013, 09:58 PM »
:-\

I keep trying then.
:-*

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Regular Expressions (help)
« Reply #19 on: January 13, 2013, 10:58 AM »
Code: (autoit)
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

;$inclip = "***CONTRO*** February 28, 2010 at 5:18am"
;$inclip = '01.02.2010.05.18'

HotKeySet('^#!2', '_Doit') ; Win+Alt+Ctrl+2

While 1
	Sleep(1000)
WEnd


Exit

Func _Doit()
	$inclip = ClipGet()

	If StringLeft($inclip, 12) <> '***CONTRO***' Then
		$outclip = StringRegExpReplace($inclip, '(..)\.(..)\.(....)\.0?(.+)\.(..)', '\1_\2_\3 , \4_\5')
	Else
		$outclip = _Valmorphanize(StringMid($inclip, 14))
	EndIf

	ClipPut($outclip)
	Sleep(50)
	Send('^v')
EndFunc

Func _Valmorphanize($temp)
	Local $hour = StringLeft(StringRight($temp, 7), StringInStr(StringRight($temp, 7), ':') - 1) + (12 * (StringRight(StringRight($temp, 7), 2) = 'pm'))
	If Not Mod($hour, 12) Then $hour-=12
	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')
EndFunc
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Re: Regular Expressions (help)
« Reply #20 on: January 13, 2013, 03:12 PM »
Code: (autoit)
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

;$inclip = "***CONTRO*** February 28, 2010 at 5:18am"
;$inclip = '01.02.2010.05.18'

HotKeySet('^#!2', '_Doit') ; Win+Alt+Ctrl+2

While 1
	Sleep(1000)
WEnd


Exit

Func _Doit()
	$inclip = ClipGet()

	If StringLeft($inclip, 12) <> '***CONTRO***' Then
		$outclip = StringRegExpReplace($inclip, '(..)\.(..)\.(....)\.0?(.+)\.(..)', '\1_\2_\3 , \4_\5')
	Else
		$outclip = _Valmorphanize(StringMid($inclip, 14))
	EndIf

	ClipPut($outclip)
	Sleep(50)
	Send('^v')
EndFunc

Func _Valmorphanize($temp)
	Local $hour = StringLeft(StringRight($temp, 7), StringInStr(StringRight($temp, 7), ':') - 1) + (12 * (StringRight(StringRight($temp, 7), 2) = 'pm'))
	If Not Mod($hour, 12) Then $hour-=12
	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')
EndFunc

Running to tryyyyy
:-*

Original address: https://www.donationcoder.com/forum/index.php?topic=33577.0