Jump to content

Solved! StringRegExpReplace Left 0 Padding: Solved!

Go to solution Solved by corgano,

Recommended Posts

Is it possible to left pad a matched group? 
Here is my situation. I am working on a program to integrate inventory from my companies warehouse with our website. 
Our part numbers in our Warehouse inventory look like this:
1501895, 1000973, 5000165, 6000002, etc.
On our website, these same numbers would look like this:
15-1895, 1-973,5-165, 6-02
My program monitors our warehouse inventory database for changes, and when a change is made it needs to find the matching item on our website.
This is the code that I originally used:


However I noticed this does not work for numbers that match my last example. The last matched group needs to be zero padded if the number is less than 10.
Does anyone have an idea of where to start for this? Is there a better way than how I have started?

Edited by LordBoling
Link to post
Share on other sites

#include <Array.au3>
Local $aValues[4][2]=[["1501895",""],["1000973",""],["5000165",""],["6000002",""]]

For $i = 0 To UBound($aValues)-1
    $aTemp = StringRegExp($aValues[$i][0],"([1-9]{1,2})(0*)(\d{1,4})",3)
    If StringLen($aTemp[2]) = 1 Then $aTemp[2] = 0 & $aTemp[2]
    $aValues[$i][1] =$aTemp[0] & "-" & $aTemp[2]
    ConsoleWrite($aValues[$i][1] & @CRLF)

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to post
Share on other sites


Non-SRE solution...

#include <string.au3>

Local $aWebValues[4]=["15-1895", "1-973","5-165", "6-02"]

For $1 = 0 To UBound($aWebValues)-1
    $aWebValues[$1] = stringreplace($aWebValues[$1],'-',_stringrepeat('0',8-stringlen($aWebValues[$1])))
    ConsoleWrite($aWebValues[$1] & @CRLF)


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to post
Share on other sites
  • Solution

would not this work?


changed the third one to d{2,4} because you want at least two digits? :P

Also, http://gskinner.com/RegExr/. Save it, bookmark it, love it. It's like the SciTE of regex

Edited by corgano


Link to post
Share on other sites

Kidney: Sorry if I was not clear. My last example, 6000002, is an example of a number that did not work with my code.

jdelaney: Thank you! Your solution works perfectly. I was just too focused on RegExReplace and didn't think about using the standard RegEx to return an array.

kylomas: Thank you as well. I am not sure what Non-SRE stands for, but your solution works great when I need to check numbers from the website.

corgano: Well, I guess I should have tried that. It was the first thing that I considered, but I thought (0*) is a greedy match and that it would stop the next group from selecting a 0. Instead of assuming I should have checked. That is the simplest solution to my problem I believe.

I appreciate everyone getting back to me so quickly.

Link to post
Share on other sites

And using StringFormat function to determine the number of zeros.

Local $new_items[4][2] = [["15-1895", ""],["1-973", ""],["5-165", ""],["6-02", ""]]

For $i = 0 To UBound($new_items) - 1
    $aTemp = StringSplit($new_items[$i][0], "-", 2)
    $new_items[$i][0] = StringFormat("%i%0" & (7 - StringLen($aTemp[0])) & "i", $aTemp[0], $aTemp[1])
    ConsoleWrite($new_items[$i][0] & @CRLF)
Link to post
Share on other sites


non-SRE = not string regular expression.

At some point you need to render one of your numbers to match the format of the other.  Offered a solution looking at it from the "other" side...

Good Luck,


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By WhaleJesus
      #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <file.au3> ; Create Data Folder if it doesn't exist yet If FileExists(@ScriptDir & "\Data") Then Else ShellExecute(@ScriptDir) DirCreate(@ScriptDir & "\Data") EndIf ; Playlist Name & location input Global $playlistnameinput = InputBox("Playlist", "Enter The playlist name", _ "Name") Global $playlistlocationinput = InputBox("Location", "Specify where you would like the playlist folder to be stored", @ScriptDir & "\Playlists\" & $playlistnameinput) ; Create file in Data folder and other vars Global $sDataFile = @ScriptDir & "\Data\Data.txt" Global $DataHandle = FileOpen($sDataFile, 1) Global $DataFileLine = FileReadLine($sDataFile, 1) FileClose($DataFileLine) MsgBox(0, "", $DataFileLine, 10) ; Prove it exists If FileExists($sDataFile) Then _FileWriteToLine($DataHandle, $DataFileLine, $playlistnameinput, True, True) $DataFileLine += 1 _FileWriteToLine($DataHandle, 1, $DataFileLine, True) Else MsgBox($MB_SYSTEMMODAL, "Error", "File " & $sDataFile & "Does not exist") EndIf Global $sPDataFile = @ScriptDir & "\Data\" & $playlistnameinput & "_Data.txt" Global $PDataHandle = FileOpen($sPDataFile, 1) If FileExists($sPDataFile) Then _FileWriteToLine($PDataHandle, 1, $playlistnameinput, True, True) _FileWriteToLine($PDataHandle, 2, $playlistlocationinput, True, True) Else MsgBox($MB_SYSTEMMODAL, "Error", "File " & $sPDataFile & "Does not exist") EndIf _FileWriteToLine stopped working and i don't know what it is in my code that's causing this, please help
    • By DannyJ
      $sCommands1 = 'powershell.exe Get-ChildItem' $iPid = run($sCommands1   , @WorkingDir , @SW_SHOW , 0x2) $sOutput = ""  While 1     $sOutput &= StdoutRead($iPID)         If @error Then             ExitLoop         EndIf  WEnd ;~ msgbox(0, '' , $sOutput) ConsoleWrite("$sOutput") ConsoleWrite($sOutput) ConsoleWrite(@CRLF) $aOutput = stringsplit($sOutput ,@LF , 2) For $i=0 To  UBound($aOutput) - 1 Step 1     ConsoleWrite($aOutput[$i]) Next The script above reads the whole directory into a one dimensional array, but I need to work with the array, so I need to split the array into multiple dimensions.
      I have already read some forum answers here, and I have already tried these commands:
      Are there any way to use the $aOutput variable like in PowerShell:
      $a = Get-ChildItem $a.Mode I imagine this in AutoIt  $aOutput
      ConsoleWrite($aOutput[i].Mode) Or if I split this command into 2 dimension like:
      For $i To UBound($aOutput)-1 Step 1 ConsoleWrite($aOutput[$i][1]) ConsoleWrite($aOutput[$i][2]) Next  
    • By DannyJ
      If I run this code, it works perfectly
      $CmdPid = Run("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit " & 'Get-ChildItem',@DesktopDir, @SW_SHOW) But this code
      $CmdPid = Run("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit " & 'Get-RDUserSession',@DesktopDir, @SW_SHOW) I get this error:
      Get-RDUserSession : The term 'Get-RDUserSession' is not recognized as the name of a cmdlet, function, script file, or o perable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try aga in. If I try run the command Get-RDUserSession  in normal PowerShell (started from windows start menu) the command works perfectly.
      But If I run with AutoIt I get the above mentioned error .
      Any ideas?
    • By Automania
      Hi all,
      I haven't used AutoIt in more than 10 years and I am sure a lot has improved since that long time. I hope you can give me some suggestions on my approach.
      Task: I need to extract user data (for around 1700 users) from a website tool. That tool shows an output in a table on the website. However, no export feature is available and I need the data in an Excel file, such as:
      username, serial number (of a laptop), ID number (of laptop) and some more
      With my knowledge from 2009 I would do this:
      1) use _IEextract with each username in the url to get the whole source code of the website with the user's data summary
      2) Work with lots of regexpressions to extract each data piece, save them into variables/array
      3) Write variable values into an Excel file
      4) rinse repeat 1700 times
      The relevant line for step 3 looks like this:
      <td class="resultcell"><span class="new">2021-03-23 11:05:00</span></td><td class="resultcell">Hostname-1234</td><td class="resultcell"><a href="?&Search=Search&result=summarized%20history&field=serial%20numbers&criteria=123456">123456</a></td><td class="resultcell">0987654/td><td class="resultcell"><a href="?&Search=Search&result=summarized%20history&field=usernames&criteria=myusername">myusername</a> and so on.. so here it would be Hostname-1234, 0987654 and myusername that I would need to extract.

      Although this may work it does not appear very efficient and would take a while. So I am happy for an alternate approach. Preferably, without using additional exe binary files due to company policies besides AutoIt itself.
    • By SEuBo
      I am just getting started with C and C++. I have created a pretty simple C code which is calling a dll function.
      When I compile and run, I get the appropriate Output. So it works fine.

      Now I would want to transform that to AutoIt. -> I would like to call the "RfcOpenConnection" function from AutoIt - but whatever I try with DLLCall, I can not get it to work. 
      Can someone point me in the right direction? DLL, C Sourcecode and compiled exe are attached too large to be attached, so they're uploaded here: 
      Thanks a bunch!
  • Create New...