Jump to content

Trim Right To RegEp


 Share

Recommended Posts

Hi all

Im trying to remove sme chars at the end of a address

e.g.

C:\Program Files (x86)\DAEMON Tools Lite\uninst.exe

$soft_path = RegRead($path_2, $StringSecond)
    $InstallLocation = StringTrimRight($soft_path, $CharRemove)

soft path is the example above and $CharRemove is just a number by counting the stuff to remove and adding the number to trim

I would like to automate it so it auto removes everything upto the last "\"

e.g.

C:\Program Files (x86)\DAEMON Tools Lite\uninst.exe

becomes

C:\Program Files (x86)\DAEMON Tools Lite\

So i started looking at stringrexep and omg talk about complicated...

im here atm but im lost

$InstallLocation = StringRegExp($soft_path,"\x\\")

Also for a different part of the script ive searched high and low for a snippet that i think GeoSoft posted that auto added a "\" if a line didnt have it at the end, can anyone point me in the direction of where that is please. Or to where a similar thing is.

Many thanks

Edited by Chimaera
Link to comment
Share on other sites

I'm not a RegEx expert but this seems to be working:

$soft_path = "C:\Program Files (x86)\DAEMON Tools Lite\uninst.exe"
$InstallLocation = StringRegExpReplace($soft_path, "^(.*)\\.*$", "$1")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $InstallLocation = ' & $InstallLocation & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Have a look at these OR in WinAPIEx.au3 look at _WinAPI_PathRemoveFileSpec() :graduated:

Edit: In all my scripts I now use the _WinAPI_Path* functions to get extension, filename etc... Look here >>

Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

$InstallLocation = StringRegExpReplace($soft_path, "^(.*)\\.*$", "$1")

This for me removes the trailing slash "\" , i need to leave the trailing slash in like my example

C:\Program Files (x86)\DAEMON Tools Lite\

Or Is it better coding practice to have the link without the trailing slash and add & "\" at the end if i need it, or always add it with the nxt section

also what does the $ do in this bit

"^(.*)\\.*$"

as i cant find it in the regexp help ?

Edited by Chimaera
Link to comment
Share on other sites

Use this instead:

$InstallLocation = StringRegExpReplace($soft_path, "^(.*)\\.*$", "$1\\")

^ means start of the string

$ means end of the string

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thanks for that UEZ thats that one sorted

For the other one which is just to add a trailing slash to the end of it if the string is missing.

C:\Program Files\CCleaner\ <<< does nothing

C:\Program Files\CCleaner << adds "\" to the end

Ive got this from the examples from guinness and looking at what you gave me which adds the slash

$InstallLocation = StringRegExpReplace($soft_path, "^(.*).*$", "$1\\")

but it adds the slash even if the original has it

so how do i get it to work out if the "\" exists and then decided to add?

Link to comment
Share on other sites

Look at or _WinAPI_PathAddBackslash()

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Im trying not to use big stuff i don't understand like winapi but i can use that function to keep me ticking over till i figure other options

Or until i end up going a different path which happens often.... ;):):graduated:

Thanks for the help all

Link to comment
Share on other sites

The one from UEZ should be giving you exactly what you asked for.

If it's not then you can try this for your replace expression.

^(.+[[:alnum:]\h])\\+.*$

Also remember that you can nest expressions. so if there is still a chance that it could mess up then (Using UEZs expression).

$InstallLocation = StringRegExpReplace(StringRegExpReplace($soft_path, "^(.*).*$", "$1\\"), "\\+", [url=""]\\[/url])

That should remove any multiple "\"s from the string.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • 2 weeks later...

Thanks for the help

The only issue ive come up against is

Im using this

$sInstallLocation = StringRegExpReplace($soft_path, "^(.*)\\.*$", "$1")

which is removing everything upto and including the last slash which is fine but..

Ive come across a few registry keys like this which it doesnt seem to like and i cant work out why?

"C:\Program Files (x86)\Universal Extractor\unins000.exe"

I think its the "" thats causing the problem, ive checked all the code upto that point and its all fine.

im still trying to find the problem but any suggestions to look at will be fine

Edited by Chimaera
Link to comment
Share on other sites

"\\" in a RegExp pattern will match an actual backslash character.

"( ... )" is a capturing group. Everything that matches within the brackets is captured and can be (back) referenced with "\1" or "$1" or "${1}".

Notice if "\\" is outside the captured group, the trailing backslash is missing.

If the "\\" is inside the captured group, the trailing backslash is present. The backslash has been captured.

Local $soft_path = "C:\Program Files (x86)\Universal Extractor\unins000.exe"

Local $sInstallLocation   = StringRegExpReplace($soft_path, "^(.*)\\.*$", "$1")
Local $sInstallLocationBS = StringRegExpReplace($soft_path, "^(.*\\).*$", "$1")

MsgBox(0, "Results", '"^(.*)\\.*$" ' & $sInstallLocation & @LF & '"^(.*\\).*$" ' & $sInstallLocationBS)
Link to comment
Share on other sites

Hi,

If you mean the the value you read from the registry includes the double quotes this should work

$soft_path = '"C:\Program Files (x86)\Universal Extractor\unins000.exe"'
$sInstallLocation = StringRegExpReplace($soft_path, "^['""]?(.*?)[^\\]*['""]?$", "$1") ; now includes the trailing backslash
ConsoleWrite("$sInstallLocation = " & $sInstallLocation & @CRLF)

Edit: small change to include single quotes

Edited by Robjong
Link to comment
Share on other sites

Probably not explaining it right

The result i get from the registry is a uninstall string

"C:\Program Files (x86)\Universal Extractor\unins000.exe"

Which somehow messes up my code, ive tried a key without the "" and it works fine so im assuming its the "" that mess it up so i want to do a simple StringRegExpExplace to remove the " from each end

and end up with this

C:\Program Files (x86)\Universal Extractor\unins000.exe

i tried this but it doesnt seem to help

$sInstallLocation = StringRegExpReplace($soft_path, '^(.*)".*$', '$1')

This doesn't need to worry about the trailing slash at all as i don't think i can combine them now, so i will run this in a func to remove the "" prior to the other one

hmm

is this right?

StringRegExpReplace($soft_path, "^['""]?['""]?$", "$1")
Edited by Chimaera
Link to comment
Share on other sites

Is there something wrong with a simple StringReplace($sStr, '"', "") ?

The RegEx is best expressed as StringRegExpReplace($sStr, "^\x22?(.+)\x22?$", "$1")

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...