Jump to content
HurleyShanabarger

RegExp - Add leading zero

Recommended Posts

HurleyShanabarger

Hey,

another thing I would like to resolve using RegExp. I have a csv with an date entry per row in the format MM/DD/YYYY h:m:s (AM/PM). As this is not really the format I want I change the order with an regular expression in the complete file into YYYY/MM/DD h:ms: (AM/PM).

Is there a way that I can add directly the leading zero on the month/day/hour using an regular expression? I know that is possible with the date functions but that is rater slow on files with more then 30000 lines.

What I got:

19,"9/4/2017 3:19:57 PM","-2:00",,"Screen","7000",,B7fspf
20,"9/4/2017 3:19:58 PM","-2:00",,"Screen","7710",,qmhkNN
21,"9/4/2017 3:20:27 PM","-2:00",,"Screen","9990",,9ui/GX
22,"9/4/2017 3:32:53 PM","-2:00",,"Screen","7710",,J4jAYc
23,"9/5/2017 6:22:02 AM","-2:00",,"Screen","9900",,nSI+V0
24,"9/5/2017 6:22:05 AM","-2:00",,"Alarm: 9538",,,j8V9qI
25,"9/5/2017 6:22:05 AM","-2:00",,"Screen","7710",,C2fSO3
26,"9/5/2017 6:22:08 AM","-2:00",,"Screen","9900",,jmwr/j

What I want:

19,"2017/09/04 03:19:57 PM","-2:00",,"Screen","7000",,B7fspf
20,"2017/09/04 03:19:58 PM","-2:00",,"Screen","7710",,qmhkNN
21,"2017/09/04 03:20:27 PM","-2:00",,"Screen","9990",,9ui/GX
22,"2017/09/04 03:32:53 PM","-2:00",,"Screen","7710",,J4jAYc
23,"2017/09/05 06:22:02 AM","-2:00",,"Screen","9900",,nSI+V0
24,"2017/09/05 06:22:05 AM","-2:00",,"Alarm: 9538",,,j8V9qI
25,"2017/09/05 06:22:05 AM","-2:00",,"Screen","7710",,C2fSO3
26,"2017/09/05 06:22:08 AM","-2:00",,"Screen","9900",,jmwr/j

What I got:

Dim $sString
$sString &= '19,"9/4/2017 3:19:57 PM","-2:00",,"Screen","7000",,B7fspf' & @CRLF
$sString &= '20,"9/4/2017 3:19:58 PM","-2:00",,"Screen","7710",,qmhkNN' & @CRLF
$sString &= '21,"9/4/2017 3:20:27 PM","-2:00",,"Screen","9990",,9ui/GX' & @CRLF
$sString &= '22,"9/4/2017 3:32:53 PM","-2:00",,"Screen","7710",,J4jAYc' & @CRLF
$sString &= '23,"9/5/2017 6:22:02 AM","-2:00",,"Screen","9900",,nSI+V0' & @CRLF
$sString &= '24,"9/5/2017 6:22:05 AM","-2:00",,"Alarm: 9538",,,j8V9qI ' & @CRLF
$sString &= '25,"9/5/2017 6:22:05 AM","-2:00",,"Screen","7710",,C2fSO3' & @CRLF
$sString &= '26,"9/5/2017 6:22:08 AM","-2:00",,"Screen","9900",,jmwr/j'
$sString = StringRegExpReplace($sString, '([0-9]*)\/([0-9]*)\/([0-9]*)\h([0-9]*)\:([0-9]*)\:([0-9]*)\h(AM|PM)', '$3/$1/$2 $4:$5:$6 $7', 0)
ConsoleWrite($sString &@CRLF)

 

Thanks again!

Share this post


Link to post
Share on other sites
jguinch

Try this one :

Local  $sString
$sString &= '19,"9/4/2017 3:19:57 PM","-2:00",,"Screen","7000",,B7fspf' & @CRLF
$sString &= '20,"9/4/2017 3:19:58 PM","-2:00",,"Screen","7710",,qmhkNN' & @CRLF
$sString &= '21,"9/12/2017 3:20:27 PM","-2:00",,"Screen","9990",,9ui/GX' & @CRLF
$sString &= '22,"12/4/2017 3:32:53 PM","-2:00",,"Screen","7710",,J4jAYc' & @CRLF
$sString &= '23,"9/5/2017 6:22:02 AM","-2:00",,"Screen","9900",,nSI+V0' & @CRLF
$sString &= '24,"9/5/2017 6:22:05 AM","-2:00",,"Alarm: 9538",,,j8V9qI ' & @CRLF
$sString &= '25,"9/5/2017 6:22:05 AM","-2:00",,"Screen","7710",,C2fSO3' & @CRLF
$sString &= '26,"9/5/2017 6:22:08 AM","-2:00",,"Screen","9900",,jmwr/j'

$sString = StringRegExpReplace($sString, '(\d+)/(\d+)/(\d+)', "$3/$1/$2")
$sString = StringRegExpReplace($sString, '[/ :]\K(?=[1-9]\b)', "0")
ConsoleWrite($sString &@CRLF)

 

Edited by jguinch

Share this post


Link to post
Share on other sites
Simpel

@jguinch, you forgot the leading 0 at the hours:

Local  $sString
$sString &= '19,"9/4/2017 3:19:57 PM","-2:00",,"Screen","7000",,B7fspf' & @CRLF
$sString &= '20,"9/4/2017 3:19:58 PM","-2:00",,"Screen","7710",,qmhkNN' & @CRLF
$sString &= '21,"9/12/2017 3:20:27 PM","-2:00",,"Screen","9990",,9ui/GX' & @CRLF
$sString &= '22,"12/4/2017 3:32:53 PM","-2:00",,"Screen","7710",,J4jAYc' & @CRLF
$sString &= '23,"9/5/2017 6:22:02 AM","-2:00",,"Screen","9900",,nSI+V0' & @CRLF
$sString &= '24,"9/5/2017 6:22:05 AM","-2:00",,"Alarm: 9538",,,j8V9qI ' & @CRLF
$sString &= '25,"9/5/2017 6:22:05 AM","-2:00",,"Screen","7710",,C2fSO3' & @CRLF
$sString &= '26,"9/5/2017 11:22:08 AM","-2:00",,"Screen","9900",,jmwr/j'

$sString = StringRegExpReplace($sString, '(\d+)/(\d+)/(\d+)', "$3/$1/$2")
$sString = StringRegExpReplace($sString, '/\K(?=[1-9]\b)', "0")
$sString = StringRegExpReplace($sString, ' \K(?=[1-9]\b)', "0")
ConsoleWrite($sString &@CRLF)

Last StringRegExpReplace fills 0 at hours. Conrad


SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites
Simpel

Jap. More elegant then mine and you thought about the minutes too.


SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites
iamtheky

What about only testing $4 for items with a single number followed by a colon, and then replacing those with the format desired?

Is there a way to make the latter part optional to the whole of the regex, so they are matched independently of one another?  currently i can do one or the other, but i cant believe JG is going to be relegated to two regexes

Local  $sString
$sString &= '19,"9/4/2017 3:19:57 PM","-2:00",,"Screen","7000",,B7fspf' & @CRLF
$sString &= '20,"9/4/2017 6:19:58 PM","-2:00",,"Screen","7710",,qmhkNN' & @CRLF
$sString &= '21,"9/12/2017 7:20:27 PM","-2:00",,"Screen","9990",,9ui/GX' & @CRLF
$sString &= '22,"12/4/2017 8:32:53 PM","-2:00",,"Screen","7710",,J4jAYc' & @CRLF
$sString &= '23,"9/5/2017 9:22:02 AM","-2:00",,"Screen","9900",,nSI+V0' & @CRLF
$sString &= '24,"9/5/2017 10:22:05 AM","-2:00",,"Alarm: 9538",,,j8V9qI ' & @CRLF
$sString &= '25,"9/5/2017 11:22:05 AM","-2:00",,"Screen","7710",,C2fSO3' & @CRLF
$sString &= '26,"9/5/2017 12:22:08 AM","-2:00",,"Screen","9900",,jmwr/j'

$sString = StringRegExpReplace($sString, '(\d+)/(\d+)/(\d+)\W(\d):', "$3/$1/$2 0$4:")

ConsoleWrite($sString &@CRLF)

 

Edited by iamtheky
  • Like 1

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
mikell

:)

Local  $sString
$sString &= '19,"9/4/2017 3:19:57 PM","-2:00",,"Screen","7000",,B7fspf' & @CRLF
$sString &= '20,"9/4/2017 3:19:58 PM","-2:00",,"Screen","7710",,qmhkNN' & @CRLF
$sString &= '21,"9/12/2017 3:20:27 PM","-2:00",,"Screen","9990",,9ui/GX' & @CRLF
$sString &= '22,"12/4/2017 3:32:53 PM","-2:00",,"Screen","7710",,J4jAYc' & @CRLF
$sString &= '23,"9/5/2017 6:22:02 AM","-2:00",,"Screen","9900",,nSI+V0' & @CRLF
$sString &= '24,"9/5/2017 6:22:05 AM","-2:00",,"Alarm: 9538",,,j8V9qI ' & @CRLF
$sString &= '25,"9/5/2017 6:22:05 AM","-2:00",,"Screen","7710",,C2fSO3' & @CRLF
$sString &= '26,"9/5/2017 6:22:08 AM","-2:00",,"Screen","9900",,jmwr/j'

$sString = Execute("'" & StringRegExpReplace($sString, '(\d+)/(\d+)/(\d+)\h+(\d+)',  _ 
    "$3/' & StringFormat('%02i', '$1') & '/' & StringFormat('%02i', '$2') & ' ' & StringFormat('%02i', '$4') & '") & "'")

Msgbox(0,"", $sString)

 

Edited by mikell
  • Like 1

Share this post


Link to post
Share on other sites
jguinch

@iamtheky : well, just one expression in that case is really complicated than two smalls. :sweating:

I thought about whether it was possible in one regex... So yes, it's possible, but not really easy. Here is one way (of course, it has no sense to use it, it's just for the fun...) ^_^

Local  $sString
$sString &= '19,"9/4/2017 3:19:57 PM","-2:00",,"Screen","7000",,B7fspf' & @CRLF
$sString &= '20,"9/4/2017 3:19:58 PM","-2:00",,"Screen","7710",,qmhkNN' & @CRLF
$sString &= '21,"9/12/2017 3:20:27 PM","-2:00",,"Screen","9990",,9ui/GX' & @CRLF
$sString &= '22,"12/4/2017 3:2:53 PM","-2:00",,"Screen","7710",,J4jAYc' & @CRLF
$sString &= '23,"9/5/2017 6:22:02 AM","-2:00",,"Screen","9900",,nSI+V0' & @CRLF
$sString &= '24,"9/5/2017 26:22:05 AM","-2:00",,"Alarm: 9538",,,j8V9qI ' & @CRLF
$sString &= '25,"9/5/2017 6:22:05 AM","-2:00",,"Screen","7710",,C2fSO3' & @CRLF
$sString &= '26,"9/5/2017 6:22:8 AM","-2:00",,"Screen","9900",,jmwr/j'

$sOut = StringRegExpReplace($sString & "0", '(?msx)  ^' & _                           ; I add a "0" at the end of the string (used to capture it if "0" is not present in the original string)
                                    '' & _                                            ; Before capturing values, I capture "0" if needed :
                                    '(?= [^"]+"     (?| ()\d{2} | \d\/.+?(0) ) )' & _   ; captures () if MM or (0) if M   =>  $1
                                    '(?= [^\/]+\/   (?| ()\d{2} | \d\/.+?(0) ) )' & _   ; captures () if DD or (0) if D   =>  $2
                                    '(?= \H+\h      (?| ()\d{2} | \d:.+?(0)  ) )' & _   ; captures () if hh or (0) if h   =>  $3
                                    '(?= [^:]+:     (?| ()\d{2} | \d:.+?(0)  ) )' & _   ; captures () if mm or (0) if m   =>  $4
                                    '(?= [^:]+:\d+: (?| ()\d{2} | \d\h.+?(0) ) )' & _   ; captures () if ss or (0) if s   =>  $5
                                    '[^"]+"(\d+)\/(\d+)\/(\d+)\h(\d+):(\d+):(\d+)(\V+)(?:(?=\R)|0\Z)', _ ; Now, captures the values, and exclude the trailing 0 at the end
                                    '$8/$1$6/$2$7 $3$9:$4$10:$5$11$12')               ; And the Replacement
ConsoleWrite($sOut)

edit : @mikell cheated, he uses 2 functions in one line (5 with StringFormat) :P

Edited by jguinch

Share this post


Link to post
Share on other sites
HurleyShanabarger

You guys are amazing. Once I saw the inline Execute it triggered me and i created this to directly change from AM/PM to 24h format (which I was actually was doing later in the script using _DateAdd).

Local $sString
$sString &= '19,"9/4/2017 3:19:57 PM","-2:00",,"Screen","7000",,B7fspf' & @CRLF
$sString &= '20,"9/4/2017 3:19:58 PM","-2:00",,"Screen","7710",,qmhkNN' & @CRLF
$sString &= '21,"9/12/2017 3:20:27 PM","-2:00",,"Screen","9990",,9ui/GX' & @CRLF
$sString &= '22,"12/4/2017 3:32:53 PM","-2:00",,"Screen","7710",,J4jAYc' & @CRLF
$sString &= '23,"9/5/2017 6:22:02 AM","-2:00",,"Screen","9900",,nSI+V0' & @CRLF
$sString &= '24,"9/5/2017 6:22:05 AM","-2:00",,"Alarm: 9538",,,j8V9qI ' & @CRLF
$sString &= '25,"9/5/2017 6:22:05 AM","-2:00",,"Screen","7710",,C2fSO3' & @CRLF
$sString &= '26,"9/5/2017 6:22:08 AM","-2:00",,"Screen","9900",,jmwr/j'

$sString = StringReplace($sString, "'", Chr(1))
$sString = Execute("'" & StringRegExpReplace($sString, '(\d+)/(\d+)/(\d+)\h+(\d+)\:(\d{2})\:(\d{2}) (PM|AM)', "$3/' & StringFormat('%02i', '$1') & '/' & StringFormat('%02i', '$2') & ' ' & StringFormat('%02i', Execute(StringReplace(StringReplace('$7', 'PM', '12+'), 'AM', '0+') & '$4')) & ':$5:$6") & "'")
$sString = StringReplace($sString, "24:", "12:")
$sString = StringReplace($sString, Chr(1), "'")

MsgBox(0, "", $sString)

Really amazing what can be done with the right knowledge.

The replacement of ' is done to prevent issues within the inline execute format.

The replace of 24: is required as 12:30 PM would result in 24:30

Share this post


Link to post
Share on other sites
mikell
10 hours ago, HurleyShanabarger said:

The replace of 24: is required as 12:30 PM would result in 24:30

Hmm. In your code the problem is, 12:30 PM and 12:30 AM  both result in 12:30   :)

Local $sString
$sString &= '19,"9/4/2017 3:19:57 PM","-2:00",,"Screen","7000",,B7fspf' & @CRLF
$sString &= '22,"12/4/2017 12:32:53 PM","-2:00",,"Screen","7710",,J4jAYc' & @CRLF
$sString &= '23,"9/5/2017 6:22:02 AM","-2:00",,"Screen","9900",,nSI+V0' & @CRLF
$sString &= '24,"9/5/2017 12:22:05 AM","-2:00",,"Alarm: 9538",,,j8V9qI ' 

$sString = Execute("'" & StringRegExpReplace($sString, '(\d+)/(\d+)/(\d+)\h+(\d+)\:(\d{2})\:(\d{2}) (PM|AM)', "$3/' & StringFormat('%02i', '$1') & '/' & StringFormat('%02i', '$2') & ' ' & StringFormat('%02i', Execute(StringReplace(StringReplace('$7', 'PM', '12+'), 'AM', '0+') & Mod('$4', 12))) & ':$5:$6") & "'")

Msgbox(0,"", $sString)

Edit
@jguinch : :P

Edited by mikell

Share this post


Link to post
Share on other sites
Malkey

A modified mikell's example using one StringFormat().

Local $sString
$sString &= '19,"9/4/2017 12:9:7 PM","-2:00",,"Screen","7000",,B7fspf' & @CRLF
$sString &= '20,"9/4/2017 1:19:58 PM","-2:00",,"Screen","7710",,"qmhkNN"' & @CRLF
$sString &= '21,"9/12/2017 2:20:27 PM","-2:00",,"Screen","9990",,9ui/GX' & @CRLF
$sString &= '22,"12/4/2017 11:32:53 PM","-2:00",,"Screen","7710",,J4jAYc' & @CRLF
$sString &= '23,"9/5/2017 1:22:02 AM","-2:00",,"Screen","9900",,nSI+V0' & @CRLF
$sString &= '24,"9/5/2017 6:22:05 AM","-2:00",,"Alarm: 9538",,,j8V9qI ' & @CRLF
$sString &= '25,"9/5/2017 6:22:05 AM","-2:00",,"Screen","7710",,C2fSO3' & @CRLF
$sString &= '26,"9/5/2017 12:22:08 AM","-2:00",,"Screen","9900",,jmwr/j'

$sString = Execute("'" & StringRegExpReplace($sString, '(\d+)/(\d+)/(\d+)\h+(\d+)\:(\d+)\:(\d+) (PM|AM)', _
        "' & StringFormat('%i/%02i/%02i %02i:%02i:%02i', '$3', '$1', '$2', Execute(StringReplace(StringReplace('$7', 'PM', '12+'), 'AM', '0+') & Mod('$4', 12)), '$5', '$6') & '") & "'")

$sString = StringRegExpReplace($sString, '(?m)([^,"\v]+)$', '"$1"') ; Add double quotes around last comma separated entry in each line, if needed.
MsgBox(0, "", $sString)

 

Share this post


Link to post
Share on other sites
jguinch

Last try, for the 24h format :

Local $sString
$sString &= '19,"9/4/2017 3:19:57 PM","-2:00",,"Screen","7000",,B7fspf' & @CRLF
$sString &= '20,"9/4/2017 3:19:58 PM","-2:00",,"Screen","7710",,qmhkNN' & @CRLF
$sString &= '21,"9/12/2017 3:20:27 PM","-2:00",,"Screen","9990",,9ui/GX' & @CRLF
$sString &= '22,"12/4/2017 3:32:53 PM","-2:00",,"Screen","7710",,J4jAYc' & @CRLF
$sString &= '23,"9/5/2017 6:22:02 AM","-2:00",,"Screen","9900",,nSI+V0' & @CRLF
$sString &= '24,"9/5/2017 6:22:05 AM","-2:00",,"Alarm: 9538",,,j8V9qI ' & @CRLF
$sString &= '25,"9/5/2017 6:22:05 AM","-2:00",,"Screen","7710",,C2fSO3' & @CRLF
$sString &= '26,"9/5/2017 6:22:08 AM","-2:00",,"Screen","9900",,jmwr/j'

Local $sAddString = '001020304050607080910112131415161718192021222324'
Local $sPattern = '(?msx)  ^' & _
                  '(?= [^"]+"' & _
                  '   (?|' & _
                  '      (\d)\/.+?(0\1) |' & _
                  '      ((\d\d))\/' & _
                  '   )' & _
                  ')' & _
                  '(?= [^\/]+\/' & _
                  '   (?|' & _
                  '      (\d)\/.+?(0\3) |' & _
                  '      ((\d\d))\/' & _
                  '   )' & _
                  ')' & _
                  '(?= \H+\h' & _
                  '   (?|' & _
                  '      (\d):\d+:\d+\hAM.+?(0\5) |' & _
                  '      (12):\d+:\d+\hAM.+?(00) |' & _
                  '      (3):\d+:\d+\hPM.+?(15) |' & _
                  '      (4):\d+:\d+\hPM.+?(16) |' & _
                  '      (5):\d+:\d+\hPM.+?(17) |' & _
                  '      (6):\d+:\d+\hPM.+?(18) |' & _
                  '      (7):\d+:\d+\hPM.+?(19) |' & _
                  '      (8):\d+:\d+\hPM.+?(20) |' & _
                  '      (9):\d+:\d+\hPM.+?(21) |' & _
                  '      (10):\d+:\d+\hPM.+?(22) |' & _
                  '      (11):\d+:\d+\hPM.+?(23) |' & _
                  '      ((12)):\d+:\d+\hPM |' & _
                  '      ((\d\d)):' & _
                  '   )' & _
                  ')' & _
                  '(?= [^:]+:' & _
                  '   (?|' & _
                  '      (\d):.+?(0\7) |' & _
                  '      ((\d\d)):' & _
                  '   )' & _
                  ')' & _
                  '(?= [^:]+:\d+:' & _
                  '   (?|' & _
                  '      (\d)\h.+?(0\9) |' & _
                  '      ((\d\d))\h' & _
                  '   )' & _
                  ')' & _
                  '[^"]+"\K\d+\/\d+\/(\d+)\h\d+:\d+:\d+\h[AP]M(\V+)(?:(?=\R)|' &  $sAddString & '\Z)'



Local $sOut = StringRegExpReplace($sString & $sAddString, $sPattern, "$11/$2/$4 $6:$8:$10$12")
ConsoleWrite($sOut)

 

Edited by jguinch

Share this post


Link to post
Share on other sites
mikell

@jguinch

bonk.gif  ...fou.gif ...    :D

Edit
May I suggest a tiny fix to this totally crazy kludge

; line #38
  (12):\d+:\d+\hPM.+?(12) |' & _

to avoid "12:30 PM" resulting in "24:30"  :)

Edited by mikell

Share this post


Link to post
Share on other sites
jguinch

@mikell : well, i'm not very good with the AM/PM hour format, but you're right. I edited my code : ((12)):\d+:\d+\hPM

This kind of replacement, using a single regex, was a good training (quite a challenge, @iamtheky ) :D

Share this post


Link to post
Share on other sites
mikell

@jguinch
Very impressive anyway, I took a looong time to decrypt this pattern ^^
But you are cheating too - hehe - you use an extra string  :whistle:

Share this post


Link to post
Share on other sites
iamtheky

I'd like to see a fully commented version (in a spoiler, so i can cheat as i work through it).  I have no idea wtf is going on with that regex.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
jguinch

@iamtheky : here is a commented version :

Spoiler
#Include <Array.au3>

Local  $sString = '' & _
                  '19,"9/4/2017 3:19:57 PM","-2:00",,"Screen","7000",,B7fspf' & @CRLF & _
                  '20,"9/4/2017 3:19:58 PM","-2:00",,"Screen","7710",,qmhkNN' & @CRLF & _
                  '21,"9/12/2017 3:20:27 PM","-2:00",,"Screen","9990",,9ui/GX' & @CRLF & _
                  '22,"12/4/2017 3:32:53 PM","-2:00",,"Screen","7710",,J4jAYc' & @CRLF & _
                  '23,"9/5/2017 6:22:02 AM","-2:00",,"Screen","9900",,nSI+V0' & @CRLF & _
                  '24,"9/5/2017 6:22:05 AM","-2:00",,"Alarm: 9538",,,j8V9qI ' & @CRLF & _
                  '25,"9/5/2017 6:22:05 AM","-2:00",,"Screen","7710",,C2fSO3' & @CRLF & _
                  '26,"9/5/2017 6:22:08 PM","-2:00",,"Screen","9900",,jmwr/j'

Local $sAddedString = '0010203040506070809101121314151617181920212223' ; This added string is needed to capture the replacement values
                                                                       ; It will be deleted in the output string

Local $sPattern = '(?mxs)' & _
'(?|                         # Two groups are captured : Group2 contains the final MM value                              ' & @CRLF & _
' ((\d\d)) |                 # Group1 and Group2 : capture MM twice                                                      ' & @CRLF & _
' (\d)(?=.+?(0\1))           # Group1 : captures a single M value, GROUP2 : captures MM (with an added zero)             ' & @CRLF & _
') \/                                                                                                                    ' & @CRLF & _
'                                                                                                                        ' & @CRLF & _
'(?|                         # Two groups are captured : Group3 contains the final DD value                              ' & @CRLF & _
' ((\d\d)) |                 # Group3 and Group4 : capture DD twice                                                      ' & @CRLF & _
' (\d)(?=.+?(0\3))           # Group3 : captures a single D value , Group4 : captures DD (with an added zero)            ' & @CRLF & _
') \/                                                                                                                    ' & @CRLF & _
'                                                                                                                        ' & @CRLF & _
'(\d{4}) \h                  # Group5 captures YYYY                                                                      ' & @CRLF & _
'                                                                                                                        ' & @CRLF & _
'(?|                         # Two groups are captured : Group7 contains the final hh value                              ' & @CRLF & _
' ((\d\d))(?=\H+\hAM)     |  # Group6 and Group7 : capture a two-number hh for a AM hour twice                           ' & @CRLF & _
' (\d)(?=\H+\hAM.+?(0\6)) |  # Group6 : captures a single h value for a AM hour, Group7 captures hh (with an added zero) ' & @CRLF & _
' (1) (?=\H+\hPM.+(13))   |  # Group6 : captures "1" for a PM hour, Group7 captures 13                                   ' & @CRLF & _
' (2) (?=\H+\hPM.+(14))   |  # Group6 : captures "2" for a PM hour, Group7 captures 14                                   ' & @CRLF & _
' (3) (?=\H+\hPM.+(15))   |  # ...                                                                                       ' & @CRLF & _
' (4) (?=\H+\hPM.+(16))   |  # ...                                                                                       ' & @CRLF & _
' (5) (?=\H+\hPM.+(17))   |  # ...                                                                                       ' & @CRLF & _
' (6) (?=\H+\hPM.+(18))   |  # ...                                                                                       ' & @CRLF & _
' (7) (?=\H+\hPM.+(19))   |  # ...                                                                                       ' & @CRLF & _
' (8) (?=\H+\hPM.+(20))   |  # ...                                                                                       ' & @CRLF & _
' (9) (?=\H+\hPM.+(21))   |  # ...                                                                                       ' & @CRLF & _
' (10)(?=\H+\hPM.+(22))   |  # ...                                                                                       ' & @CRLF & _
' (11)(?=\H+\hPM.+(23))   |  # ...                                                                                       ' & @CRLF & _
' (12)(?=\H+\hPM.+(00))   |  # ...                                                                                       ' & @CRLF & _
'):                                                                                                                      ' & @CRLF & _
'                                                                                                                        ' & @CRLF & _
'(?|                         # Two groups are captured : Group9 contains the final mm value                              ' & @CRLF & _
' ((\d\d)) |                 # Group8 and Group9 : capture mm twice                                                      ' & @CRLF & _
' (\d)(?=.+?(0\8))           # Group8 : captures a single m value, Group9 : captures mm (with an added zero)             ' & @CRLF & _
'):                                                                                                                      ' & @CRLF & _
'                                                                                                                        ' & @CRLF & _
'(?|                         # Two groups are captured : Group10 contains the final ss value                             ' & @CRLF & _
' ((\d\d)) |  # (10) (11)    # Group10 and Group11 : capture ss twice                                                    ' & @CRLF & _
' (\d)(?=.+?(0\10))          # Group10 : captures a single s value, Group11 : captures ss (with an added zero)           ' & @CRLF & _
')\h[AP]M                                                                                                                ' & @CRLF & _
'                                                                                                                        ' & @CRLF & _
'(\V+)                       # Group12 captures the rest of the line                                                     ' & @CRLF & _
'(?:(?=\R)|0010203040506070809101121314151617181920212223\Z) # Avoid the added string to be part of the replacement      '

Local $sOut = StringRegExpReplace($sString & $sAddedString, $sPattern, "$5/$2/$4 $7:$9:$11 $12")
ConsoleWrite($sOut)

Local $aMatches = StringRegExp($sString & $sAddedString, $sPattern, 3)
_ArrayDisplay($aMatches)

 

 

Edited by jguinch
  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.