JamesDover Posted October 15, 2021 Share Posted October 15, 2021 (edited) I know this is a simple script however, I can't seem to get the code correct. I'm pulling in domain users in this format mydomain\firstname.lastname and I would like the output to be firstname.lastname (basically just removing the mydomain\ ). Here is the code below that I am working on. Cheers #include <MsgBoxConstants.au3> Test1() Func Test1() Local $sInput = "mydomain\firstname.lastname" Local $sOutput = StringRegExpReplace($sInput, 'mydomain\', '$1') Display($sInput, $sOutput) EndFunc Edited October 15, 2021 by JamesDover Link to comment Share on other sites More sharing options...
Developers Solution Jos Posted October 15, 2021 Developers Solution Share Posted October 15, 2021 You need to escape the backslash: Test1() Func Test1() Local $sInput = "mydomain\firstname.lastname" Local $sOutput = StringRegExpReplace($sInput, 'mydomain\\', '$1') ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sOutput = ' & $sOutput & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console EndFunc ;==>Test1 Jos JamesDover 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Musashi Posted October 15, 2021 Share Posted October 15, 2021 (edited) #include <MsgBoxConstants.au3> Test1() Func Test1() Local $sInput = "mydomain\firstname.lastname" Local $sOutput1 = StringRegExpReplace($sInput, '(mydomain\\)', '$1') Local $sOutput2 = StringReplace($sInput, 'mydomain\', '') ConsoleWrite("Input = " & $sInput & @CRLF) ConsoleWrite("Output1 = " & $sOutput1 & @CRLF) ConsoleWrite("Output2 = " & $sOutput2 & @CRLF) EndFunc EDIT : @Jos was faster. BTW : In your case a simple StringReplace would also be sufficient. Edited October 15, 2021 by Musashi JamesDover 1 "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
JamesDover Posted October 15, 2021 Author Share Posted October 15, 2021 Excellent! Thanks for the quick responses. Link to comment Share on other sites More sharing options...
mikell Posted October 15, 2021 Share Posted October 15, 2021 Assuming that the "mydomain" part may vary : Test1() Test2() ; get what you want Func Test1() Local $sInput = "mydomain\firstname.lastname" Local $sOutput = StringRegExpReplace($sInput, '.*?\\(.*)', "$1") Msgbox(0,"1", $sOutput) EndFunc ; or fire what you don't want Func Test2() Local $sInput = "mydomain\firstname.lastname" Local $sOutput = StringRegExpReplace($sInput, '[^\\]+\\', "") Msgbox(0,"2", $sOutput) EndFunc JamesDover and Zedna 2 Link to comment Share on other sites More sharing options...
JamesDover Posted October 15, 2021 Author Share Posted October 15, 2021 23 minutes ago, mikell said: Assuming that the "mydomain" part may vary : Test1() Test2() ; get what you want Func Test1() Local $sInput = "mydomain\firstname.lastname" Local $sOutput = StringRegExpReplace($sInput, '.*?\\(.*)', "$1") Msgbox(0,"1", $sOutput) EndFunc ; or fire what you don't want Func Test2() Local $sInput = "mydomain\firstname.lastname" Local $sOutput = StringRegExpReplace($sInput, '[^\\]+\\', "") Msgbox(0,"2", $sOutput) EndFunc That's a good example. I've always struggled with StringRegExpReplace expressions so this was very helpful. Link to comment Share on other sites More sharing options...
JamesDover Posted October 15, 2021 Author Share Posted October 15, 2021 I just found one that was in this format mydomain/firstname.lastname and the StringRegExpReplace doesn't seem to effect it. Would this be because the \ is in a different direction on the StringRegExpReplace? Func Test2() Local $sInput = "mydomain/firstname.lastname" Local $sOutput = StringRegExpReplace($sInput, '[^//]+//', "") Msgbox(0,"2", $sOutput) EndFunc Link to comment Share on other sites More sharing options...
Musashi Posted October 15, 2021 Share Posted October 15, 2021 (edited) 1 hour ago, JamesDover said: Would this be because the \ is in a different direction on the StringRegExpReplace? Yes ! Metacharacters : PCRE metacharacters are \ . ^ $ | [ ( { * + ? # which have one or more special meaning, depending on context. To insert a literal metacharacter, precede it by adding a backslash (this is called escaping (or quoting) a character). / is not a metacharacter and need not be escaped with \ Test1() Test2() Func Test1() Local $sInput = "mydomain/firstname.lastname" Local $sOutput = StringRegExpReplace($sInput, '.*?/(.*)', "$1") Msgbox(0,"1", $sOutput) EndFunc Func Test2() Local $sInput = "mydomain/firstname.lastname" Local $sOutput = StringRegExpReplace($sInput, '[^/]+/', "") Msgbox(0,"2", $sOutput) EndFunc Edited October 15, 2021 by Musashi typo JamesDover 1 "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
JamesDover Posted October 18, 2021 Author Share Posted October 18, 2021 Thanks everyone. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now