# Solved! StringRegExpReplace Left 0 Padding: Solved!

## 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:

StringRegExpReplace($new_items[$i][0],"([1-9]{1,2})(0*)(\d{1,4})","$1-$3")

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

##### Share on other sites

could you give an example of what one of these numbers would be that does not work with your script currently??

##### 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) Next 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. #### Share this post ##### Link to post ##### Share on other sites LB, 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)
Next

kylomas

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

##### Share on other sites

would not this work?

StringRegExpReplace($new_items[$i][0],"([1-9]{1,2})(0*)(\d{2,4})","$1-$3")

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

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

Edited by corgano
• 2

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

##### 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.

##### 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) Next #### Share this post ##### Link to post ##### Share on other sites LB, 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, kylomas 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 #### Share this post ##### Link to post ##### Share on other sites Yaaay! My first answered I would like to thank my mom, and the bloke who wrote RegExr I <3 RegExr 0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e #### 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 • ### Similar Content • By CodingMonkey81 All, I'm working on automating the update process for a piece of software that we use at my company. The installer file always contains the version number as a part of the file name (eg InstallFile_9342.msi) and the numbers always change with every update. How do I use AutoIt to run the program? I know I can't use a wildcard in place of the numbers when using the "Run" command. I tried Local$aArray = _FileListToArray("C:\path\to\file", "InstallFile_*.msi", 0, 1) Run("$aArray") and I also tried Local$bInstall = FileFindFirstFile("InstallFile_*.msi") Run("$bInstall") and neither worked. Can someone please point me in the right direction? I've done some searching on the forum and haven't found anyone in a similar situation before. Thanks! • By RHolmes I have a program that has a control that changes color a few seconds into running. So ideally, I would poll this to tell when an event has occurred. I can't seem to retrieve the correct color value for a control. It always seems to return white indicating that its selecting somewhere else in the window. In the PixelGetColor call I'm adding half the width to the x value and subtracting half the height to the y value in order to get the center of the control. (assuming the coords returned by ControlGetPos are top left - which i can't be sure of) But I've also tried without modifying the x/y and with changing the PixelCoordMode option to 2. Maybe I'm making a silly mistake and can't see it? Any help would be appreciated. Code is below: Opt("PixelCoordMode", 0) FileChangeDir( "C:\Where\My\File\Is" ); Run( "MyProgram.exe" ) Local$hClient = WinWaitActive( $CLIENT_TITLE, "", 10 ) Local$systemIndicatorClassNN= "[CLASS:Qt5QWindowIcon; INSTANCE:99]"
Local $hSystemIndicator = ControlGetHandle ($hClient, "", $systemIndicatorClassNN) Local$xywh = ControlGetPos ( $hClient, "",$hSystemIndicator )
For $i = 10 To 1 Step -1$color = PixelGetColor ( $xywh[0] + ($xywh[2]/2), $xywh[1] - ($xywh[3]/2), $hClient ) LogToFile($color )
Sleep( 2000 )
Next
• By VAN0
Hello.
I have a program XX that asks for user confirmation on exit, which hangs the system reboot/shutdown process until I confirm. So I decided create a autoit script that would automatically close that dialog and let the system reboot/shutdown.
The problem I'm facing is that my scripts now exits before the XX.
Is there a way delay script exit until the XX is closed but only when system is rebooting/shutting down?

Thank you.
• By ViciousXUSMC
So I ran into this crazy "program" that cant be uninstalled via WMI, MSIExec, etc.
The only way to uninstall it was from Add/Remove programs manually... Or I found if you find it in the registry under HKCU and run the  uninstall string, it will also uninstall.
However the string in the registry cant be run directly in a cmd window because of the format errors.
It has spaces without quotations, it has invalid characters, etc, etc
I know things run different when executed in the registry, so maybe there is a way I can run the regsitry key just like how the system does?  If so chime in.
Otherwise I did this a crude way using several stringregexpreplace() functions and have it working.
The solution feels so barbaric and crude that I wanted to post it so some of you guys better than me can clean up the code, maybe offer alternative ways to do it, or reduce the number of times I process the string.
Here is the string right out of the registry:
c:\Program Files\Common Files\Microsoft Shared\VSTO\10.0\VSTOInstaller.exe /Uninstall file:///C:/Users/it022565/AppData/Local/Temp/OOBAXTOWordAddIn/ApplicationXtender.AXTO.Word.vsto Here is my cave man scripting to turn this into a run able string.

Func _UninstallOld() For $i = 1 to 100 ;Enumerate Registry$sEnumBase = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" ;Look in HKCU for the uninstall string for the old version $sEnum = RegEnumKey($sEnumBase, $i) If @Error Then Return If$iDebug = 1 Then MsgBox(0, "", $sEnum) If StringInStr(RegRead($sEnumBase & $sEnum, "DisplayName"), "Word Addin") Then ExitLoop Next If$iDebug = 1 Then MsgBox(0, "", $sEnum)$sKey = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sEnum$sKey2 = RegRead($sKey, "UninstallString") If$iDebug = 1 Then MsgBox(0, "Original Install Location", $sKey2)$sKey3 = StringRegExpReplace($sKey2, "(?i)(c:.*exe)", '"$1"') If $iDebug = 1 Then MsgBox(0, "",$sKey3) $sKey4 = StringRegExpReplace($sKey3, "(?i)file:///", "") If $iDebug = 1 Then MsgBox(0, "",$sKey4) $sKey5 = StringRegExpReplace($sKey4, "%20", " ") If $iDebug = 1 Then MsgBox(0, "",$sKey5) $sKey6 = StringRegExpReplace($sKey5, '(?i)((?<!")c:.*vsto)', '"$1"') If$iDebug = 1 Then MsgBox(0, "", $sKey6) RunWait(@ComSpec & ' /c ' & '"' &$sKey6 & ' /s"', "", @SW_HIDE) EndFunc Basically step by step I add quotations, strip bad characters, etc.  Kind of proud for using look behind for once
Looking forward to what you guys come up with.
• By VIP
Need help to make function better  with full infomation
#include <Array.au3> #include <File.au3> _TEST(@ScriptFullPath) _TEST("A:") _TEST("A:\B.c") _TEST("D:\E\F\") _TEST("G:\H/../J.k/") _TEST("M:\N\k..J.k") _TEST("D:\E\F\..\G\G\I..J.K.M") Func _TEST($sFilePath) Local$sDrive = "", $sFullPathDir = "",$sDirPath = "", $sDirName = "",$sFileName = "", $sFileNameExt = "",$sExtension = "", $sExt = "" Local$aPathSplit = _PathSplitByRef($sFilePath,$sDrive, $sFullPathDir,$sDirPath, $sDirName,$sFileName, $sFileNameExt,$sExtension, $sExt) ConsoleWrite("!Path IN : " &$sFilePath & @CRLF) ; C:\Windows\System32\etc\hosts.exe ConsoleWrite("- Driver : " & $sDrive & @CRLF) ; C: ConsoleWrite("- DirPath : " &$sFullPathDir & @CRLF) ; C:\Windows\System32\etc\etc ConsoleWrite("- DirPath : " & $sDirPath & @CRLF) ; \Windows\System32\etc\ ConsoleWrite("- DirName : " &$sDirName & @CRLF) ; etc ConsoleWrite("- FileName : " & $sFileName & @CRLF) ; hosts ConsoleWrite("- FileNameExt: " &$sFileNameExt & @CRLF) ; hosts.exe ConsoleWrite("- Extension : " & $sExtension & @CRLF) ; .exe ConsoleWrite("- Ext : " &$sExt & @CRLF & @CRLF) ; exe ;~ ConsoleWrite("!Path IN : " & $aPathSplit[0] & @CRLF) ; C:\Windows\System32\etc\hosts.exe ;~ ConsoleWrite("- Driver : " &$aPathSplit[1] & @CRLF) ; C: ;~ ConsoleWrite("- DirPath : " & $aPathSplit[2] & @CRLF) ; C:\Windows\System32\etc\etc ;~ ConsoleWrite("- DirPath : " &$aPathSplit[3] & @CRLF) ; \Windows\System32\etc\ ;~ ConsoleWrite("- DirName : " & $aPathSplit[4] & @CRLF) ; etc ;~ ConsoleWrite("- FileName : " &$aPathSplit[5] & @CRLF) ; hosts ;~ ConsoleWrite("- FileNameExt: " & $aPathSplit[6] & @CRLF) ; hosts.exe ;~ ConsoleWrite("- Extension : " &$aPathSplit[7] & @CRLF) ; .exe ;~ ConsoleWrite("- Ext : " & $aPathSplit[8] & @CRLF) ; exe ;~ _ArrayDisplay($aPathSplit, "_PathSplit of " & $sFilePath) EndFunc ;==>_TEST Func _PathSplitByRef($sFilePath, ByRef $sDrive, ByRef$sFullPathDir, ByRef $sDirPath, ByRef$sDirName, ByRef $sFileName, ByRef$sFileNameExt, ByRef $sExtension, ByRef$sExt) If StringInStr($sFilePath,"..") Then$sFilePath=_PathFull($sFilePath) Local$aPartOfPath=StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\$^\?\/\$+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH) ;~ If @error Then ReDim$aPartOfPath[9] ;~ $aPartOfPath[0] =$sFilePath ;~ EndIf $aPartOfPath[0] =$sFilePath ; C:\Windows\System32\etc\hosts.exe $sDrive =$aPartOfPath[1] ; C: $sFullPathDir =$aPartOfPath[1] & $aPartOfPath[2] ; C:\Windows\System32\etc If StringLeft($aPartOfPath[2], 1) == "/" Then $sDirPath = StringRegExpReplace($aPartOfPath[2], "\h*[\/\\]+\h*", "\/") Else $sDirPath = StringRegExpReplace($aPartOfPath[2], "\h*[\/\\]+\h*", "\\") EndIf $aPartOfPath[2] =$sFullPathDir ; C:\Windows\System32\etc $sDirName=StringReplace($sDirPath,"\","") $sDirName=StringReplace($sDirPath,"/","") $sFileName =$aPartOfPath[3] ; hosts $aPartOfPath[5] =$sFileName ; hosts $sExtension =$aPartOfPath[4] ; .exe $aPartOfPath[7] =$sExtension ; .exe $aPartOfPath[3] =$sDirPath ; \Windows\System32\etc\ $aPartOfPath[4] =$sDirName ; etc $aPartOfPath[6] =$sFileName & $sExtension ; hosts.exe$sFileNameExt = $aPartOfPath[6] ; hosts.exe$sExt = StringReplace($sExtension,".","") ; exe$aPartOfPath[8] = $sExt ; exe Return$aPartOfPath EndFunc ;==>_PathSplitByRef
×

• Wiki

• Back

• Git