Jump to content

Replace characters between quotes with asterisk


Mecano
 Share

Go to solution Solved by mikell,

Recommended Posts

Hallo forum members,

I am looking for a pattern to replace user= and password= between double quotes

user="Alfa75" becomes  user="*****"

password="xstsgSvs" becomes  password="*****"

In my example the opposite happens, username and password is still visable 

  StringReplace or StringRegExpReplace is not my best skill
 

Opt("MustDeclareVars", 1)

Global $Config = @ScriptDir & "\config.xml"

_Config()
Func _Config()
    If FileExists($Config) Then

        Local $file = FileOpen($Config, 0)
        Local $sData = FileRead($file)
        FileClose($file)
        Local $rData = StringRegExpReplace($sData, "(user=Chr(34)).+?(password=Chr(34)", "\1")
        ;$rData = StringReplace($rData, "user=", "*****") ; <-- desired with asterisk
        ;$rData = StringReplace($rData, "password=", "*****") ; <-- desired with asterisk
        $rData = StringReplace($rData, "user=", "")
        $rData = StringReplace($rData, "password=", "")
        MsgBox(0, "config.xml : ", $rData)
        ;ClipPut($rData)
    Else
        MsgBox(0, "config.xml : ", "Not found")
    EndIf

EndFunc

 config.xml

<config>
  <line short="LT" city="ASD" user="Alfa75" password="xstsgSvs" code="020" registered="1" period="20" />
  <line short="IC" city="RTD" user="Jonh09" password="Gdte55567" code="010" registered="1" period="25" />
  <line short="IB" city="UT" user="2ronals" password="B776656" code="030" registered="0" period="15" />
  <line short="IC" city="RTD" user="DEjong67" password="=0gdd++00L==" code="010" registered="1" period="25" />
  <line short="ST" city="AML" user="Eddy03" password=".te555=77Ai" code="036" registered="1" period="25" />
  <line short="LT" city="CAS" user="BassB" password="+gdtWU=" code="0251" registered="1" period="35" />
  <line short="IC" city="DT" user="lian033" password="77t--e55+=" code="015" registered="1" period="45" />
</config>

desired result

<config>
  <line short="LT" city="ASD" user="*****" password="*****" code="020" registered="1" period="20" />
  <line short="IC" city="RTD" user="*****" password="*****" code="010" registered="1" period="25" />
  <line short="IB" city="UT" user="*****" password="*****" code="030" registered="0" period="15" />
  <line short="IC" city="RTD" user="*****" password="*****" code="010" registered="1" period="25" />
  <line short="ST" city="AML" user="*****" password="*****" code="036" registered="1" period="25" />
  <line short="LT" city="CAS" user="*****" password="*****" code="0251" registered="1" period="35" />
  <line short="IC" city="DT" user="*****" password="*****" code="015" registered="1" period="45" />
</config>
Link to comment
Share on other sites

you are telling it to replace "user=" and "password=" with "", instead of telling it to replace the text following those words.

If @error Then
    MsgBox(262192, "", @ComputerName & " slaps " & @UserName & " around a bit with a large trout!")
EndIf

"Yeah yeah yeah patience, how long will that take?"  -Ed Gruberman

REAL search results  |  SciTE4AutoIt3 Editor Full Version

Link to comment
Share on other sites

you are telling it to replace "user=" and "password=" with "", instead of telling it to replace the text following those words.

I know, the point is I don't know the words between the double quotes, they always differed.

After user=" replace the unknown word (characters) with ***** and do the same with password="

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

×
×
  • Create New...