eliminate default values with regex

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

eliminate default values with regex

20 Sep 2018, 08:38

I try to RemoveDefaultDefinitions from Parameters. But Can't create a needle that is robust or correct.

Code: Select all

;Input
>param1 := { 1:"Test" , "Key {2": "" } , Param3 := [ {"t,e[a]s:t{b}": {1:"t,e[a]s:t{b}","t,e[a]s:t{b}": [4 ] }}]<

;this is the current result
>param1 .. --------------------------- , Param3 .. [ {--------------: ----------------------------------------}]<
;this is what I would like to achieve
>param1 .. --------------------------- , Param3 .. -------------------------------------------------------------<
example code:

Code: Select all

Text = param1 := { 1:"Test" , "Key {2": "" } , Param3 := [ {"t,e[a]s:t{b}": {1:"t,e[a]s:t{b}","t,e[a]s:t{b}": [4 ] }}]
MsgBox % ">" Text "<`n>" RemoveDefaultDefinitions(Text) "<"
ExitApp

RemoveDefaultDefinitions(Line){
  static chars := [{"open":Chr(34),"close":Chr(34)}        ; quote character
                  ,{"open":"\[",    "close":"]"}   
                  ,{"open":"\{",    "close":"}"}
                  ,{"open":":",    "close":"="}]   
  CleanLine := Line
  For k,q in chars
  {
    CleanLine := StrReplace(CleanLine, q.open . q.close , "..")
    Pos := 1     
    Needle := q.open . "[^" q.open "]*?" . q.close
    While Pos := RegExMatch(CleanLine, Needle, QuotedString, Pos){
      ReplaceString =
      Loop, % StrLen(QuotedString)
         ReplaceString .= "-"
      CleanLine := RegExReplace(CleanLine, Needle, ReplaceString, Count, 1, Pos)
    }
  }
  Return CleanLine
}
ciao
toralf
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: eliminate default values with regex

21 Sep 2018, 01:29

toralf wrote:;this is what I would like to achieve
>param1 .. --------------------------- , Param3 .. -------------------------------------------------------------<
quick fix :roll:

Code: Select all

Text = param1 := { 1:"Test" , "Key {2": "" } , Param3 := [ {"t,e[a]s:t{b}": {1:"t,e[a]s:t{b}","t,e[a]s:t{b}": [4 ] }}]
MsgBox % RemoveDefaultDefinitions(Text)
ExitApp

RemoveDefaultDefinitions(Line){
  static chars := [{"open":Chr(34),"close":Chr(34)}        ; quote character
                  ,{"open":"\[",    "close":"]"}   
                  ,{"open":"\{",    "close":"}"}
                  ,{"open":":",    "close":"="}]   
  CleanLine := Line
  loop, 2  													;  <---  the fix
  For k,q in chars
  {
    CleanLine := StrReplace(CleanLine, q.open . q.close , "..")
    Pos := 1     
    Needle := q.open . "[^" q.open "]*?" . q.close
    While Pos := RegExMatch(CleanLine, Needle, QuotedString, Pos){
      ReplaceString =
      Loop, % StrLen(QuotedString)
         ReplaceString .= "-"
      CleanLine := RegExReplace(CleanLine, Needle, ReplaceString, Count, 1, Pos)
    }
  }
  Return CleanLine
}


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: inseption86, jaka1 and 289 guests