Jump to content
Sign in to follow this  
Mecano

Replace characters between quotes with asterisk

Recommended Posts

Mecano

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>

Share this post


Link to post
Share on other sites
alienclone

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

>Exit code: 0

REAL search results

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

Share this post


Link to post
Share on other sites
Mecano

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="

Share this post


Link to post
Share on other sites
mikell

Local $rData = StringRegExpReplace($sData, '(user|password)="\K[^"]+', "****")

:)

Edited by mikell

Share this post


Link to post
Share on other sites
mikell

K says :

"Mr regex, please just forget what you have seen mentioned before me"

Very useful sometimes  :)

Share this post


Link to post
Share on other sites
Mecano

Tanks for the after service

Useful information!

:)

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
Sign in to follow this  

  • Similar Content

    • vin1
      By vin1
      i need a script that can replace words in text with other words from a words list
      it has to have 2 edit boxes input/output and a button "run"
      from word_list.txt it choses which word to replace with. words are separated with commas for multiple choice
       

       
       

       
       
      ; AutoIt GUI Example ; Created: 17/01/2005 - CyberSlug ; Modifed: 05/12/2011 - guinness ; Modifed: 09/06/2014 - mLipok #Region INCLUDE #include <AVIConstants.au3> #include <GuiConstantsEx.au3> #include <TreeViewConstants.au3> #EndRegion INCLUDE #Region GUI GUICreate("Sample GUI", 400, 420) GUISetIcon(@SystemDir & "\mspaint.exe", 0) #EndRegion GUI #Region EDIT GUICtrlCreateEdit(@CRLF & " Sample Edit Control", 10, 10, 380, 175) GUICtrlSetTip(-1, '#Region EDIT') #EndRegion EDIT #Region EDIT2 GUICtrlCreateEdit(@CRLF & " Sample Edit Control", 10, 185, 380, 175) GUICtrlSetTip(-1, '#Region EDIT') #EndRegion EDIT2 #Region BUTTON GUICtrlCreateButton("Sample Button", 10, 360, 100, 30) GUICtrlSetTip(-1, '#Region BUTTON') #EndRegion BUTTON #Region GUI MESSAGE LOOP GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd GUIDelete() #EndRegion GUI MESSAGE LOOP  
    • way1000
      By way1000
      i'm trying to create a program that let's the user type a word in an input field (gui) and when a button is pressed the letters of the word are replaced by 1 word each from a list of words in a text file
      i created the gui for it but i need help to add some more code to make it work like intented

      each letter of a word should be replaced by a word from a text file
      the text file is a source for replacing letters is a file with 2 columns separated by tab that contains the entire alphabet (26 rows)
       
      please help if possible
       
      i posted an example gui screenshot

    • ur
      By ur
      If we give the below line in our script, we can expand the variables in double quotes to its values.
      AutoItSetOption ("ExpandVarStrings", 1) Like, 
      Local $b=4 MsgBox(0,"$b$","See subject") But coming to variables, they are not getting expanded.
      Local $a[3]=[1,2,3] Local $b=4 _ArrayDisplay($a) MsgBox(0,"$a$$b$","See subject") Could anyone please help me on this.
    • Myicq
      By Myicq
      I am currently doing a project involving keeping some parameters in an internal 2d array. I am reading part of that to and from an ini file.
      As part of the reading I need to put the variables from the ini file into the 2D array. I have found _arrayinsert(), but that creates additional lines.
      Bascially I would like something as this:
      Original array:
      [1] a1 a2 a3 a4 [2] b1 b2 b3 b4 [3] c1 c2 c3 c4 And reading setting for "2" being    d1  d2  d3  d4   I should now have
      [1] a1 a2 a3 a4 [2] d1 d2 d3 d4 [3] c1 c2 c3 c4 Is there a built-in UDF or similar to do something like that already ? Or did I missing something real obvious ?
      I have rolled my own code, which works. But of course it could be improved.
      Thanks for all comments, happy to learn from you all !
       
    • uncommon
      By uncommon

      Func __ReplaceStringInFile($szFileName, $szSearchString, $szReplaceString, $fCaseness = 0, $fOccurance = 1) Local $iRetVal = 0 Local $nCount, $sEndsWith ; Check if file is readonly .. If StringInStr(FileGetAttrib($szFileName), "R") Then Return SetError(6, 0, -1) ;=============================================================================== ;== Read the file into an array ;=============================================================================== Local $hFile = FileOpen($szFileName, $FO_READ) If $hFile = -1 Then Return SetError(1, 0, -1) Local $s_TotFile = FileRead($hFile, FileGetSize($szFileName)) If StringRight($s_TotFile, 2) = @CRLF Then $sEndsWith = @CRLF ElseIf StringRight($s_TotFile, 1) = @CR Then $sEndsWith = @CR ElseIf StringRight($s_TotFile, 1) = @LF Then $sEndsWith = @LF Else $sEndsWith = "" EndIf Local $aFileLines = StringSplit(StringStripCR($s_TotFile), @LF) FileClose($hFile) ;=============================================================================== ;== Open the output file in write mode ;=============================================================================== Local $iEncoding = FileGetEncoding($szFileName) Local $hWriteHandle = FileOpen($szFileName, $iEncoding + $FO_OVERWRITE) If $hWriteHandle = -1 Then Return SetError(2, 0, -1) ;=============================================================================== ;== Loop through the array and search for $szSearchString ;=============================================================================== For $nCount = 1 To $aFileLines[0] If StringInStr($aFileLines[$nCount], $szSearchString, $fCaseness) Then $iRetVal = $iRetVal + 1 If $fOccurance = $iRetVal Then $aFileLines[$nCount] = StringReplace($aFileLines[$nCount], $szSearchString, $szReplaceString, 1 - $fOccurance, $fCaseness) ExitLoop EndIf EndIf Next ;=============================================================================== ;== Write the lines back to original file. ;=============================================================================== FileClose($hWriteHandle) Local $Tempfile = _TempFile() FileWrite($Tempfile, _ArrayToString($aFileLines, @LF, 1)) FileMove($Tempfile, $szFileName, 1) Return $iRetVal EndFunc ;==>__ReplaceStringInFile Yes, I know guinness improved the ReplaceStringInFile function in terms of speed and  efficiency but hear me out. I made some changes to the original myself that increased the speed of the writing and more importantly for me change the occurrence option so the function would only replace that specific occurrence of string.
      I not saying use my code. What I am suggesting is to add extra functionally to the ReplaceStringInFile so there is a option to replace a specific occurrence.
      I could not think of a way to do this with the new one guinness made so above is just an example of how I did it with the old one.
      I have also attached the file.au3 file.
      File.au3
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.