Jump to content

StringRegExpReplace help


Go to solution Solved by Jos,

Recommended Posts

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 by JamesDover
Link to comment
Share on other sites

  • Developers
  • Solution

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

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

#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 by Musashi

Musashi-C64.png

"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

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

 

Link to comment
Share on other sites

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

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

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 by Musashi
typo

Musashi-C64.png

"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

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