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

Of course but it's less funny  :)

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

    • Rskm
      By Rskm
      Hi, I have the following line in a text file 'input.txt'. I know the line number - say '6'. I wish to replace the text 'WWW' in the below line with a random number (I can generate that with random()).
      WERIS  WWWJP   3.83  8.330  1.000                1097.RAXX 
      The WWW is a 3 digit integer (could be any number between 0 to 999), I can use stringtrimleft and get the numerical value of WWW in this file
      so, basically, I know the string to replace (ie; WWW stored in a variable), I know the line number to work on and the file location/name and the replacement variable (through random()). My requirement is to fill that 3 spaces with my random number (which Is a integer between 1 and 999)
      please put ur suggestions
       
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I am working with Word UDF ( thanks @water! ), and, especially, with the function _Word_DocFindReplace().
      The replace does work everywhere in the document, but, it does not work in Headers or Footers.
      Am I missing something or am I forced to use the code below?
      I have already looked in the Help file ( about _Word_DocFindReplace() ), but there are no mentions about replace text in Headers/Footers.
      Sub FindAndReplaceFirstStoryOfEachType() Dim rngStory As Range For Each rngStory In ActiveDocument.StoryRanges With rngStory.Find .Text = "find text" .Replacement.Text = "I'm found .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Next rngStory End Sub Thanks everyone in advance


      Best Regards.
    • WoodGrain
      By WoodGrain
      Hi All,
      I'd like to replace 'COMMA' with ',' for example:
      $myString = "COMMA" StringRegExpReplace($myString, 'COMMA', ',') Now I've tried escaping the ',' in various ways unsuccessfully, such as:
      '[,]'
      "[,]"
      '\,'
      [,] seems to work in the pattern, I just can't figure out how to use it in the replace, and it seems everyone online is only interested in removing/replacing commas lol.
      I also tried creating and using a variable as the replacement but also didn't work:
      $myComma = "," $myString = "COMMA" StringRegExpReplace($myString, 'COMMA', $myComma) I'm sure it's super simple if someone could point me in the right direction - thanks.
    • 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

×