String Replace Issues

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

String Replace Issues

Post by CyberCitizen » Mon Jun 09, 2014 11:09 pm

Hey Guys,

Hitting a wall here. I have the below script which I use as a bit of a notepad etc. What I am trying to add is the option of removing multiple lines.

Etc I copy an email below. What I am wanting to do is remove anything that has additional spacing. Eg multiple %CRLF%'s & double %SPACE%'s. So if there is more than 2x CRLF's make it one & if there are issues like this where there is the double spaces it removes it. I have played around with String Replace, but haven't had much success. Any other ideas / suggestions.

Code: Select all

From: XXX
Sent: Friday, 6 June 2014 3:20 PM
To: XXX
Subject: Request INC-XXXReassigned From XXX

 

  Service Desk: Request Reassigned 

Dear XXX,
Reference No:	XXX
Description:	URGENT - Follow You Printing (XXX)

This request has been assigned to you by Marval user XXX with the following information: 

on review, it appears that XXX did not receive the ICT advisory for XXX on L02 XXX House.
Have found that XXX is not on the DL for L02 XXX House.
While on site, assisted client with registering for XXX.
Spoke to XXX to report the DL issue.
messaging XXX with a follow up with links for Follow-You.

Nothing further to action at this time.
solving pending further issues.

Code: Select all

// COMPILE_OPTS|C:\_tools\MJ\Text Capture.exe|M:\Icons\fonts.ico|CONSOLE=0|INCLUDES=1| /LOGFILE=\dev\nul /NOSYSTRAY /HIDE|RUNTIMES=1|BMPS=1
Let>APP_TITLE=Text Capture
Let>MSG_HEIGHT=320
Let>MSG_WIDTH=500
Let>MSG_STAYONTOP=1
Let>MSG_CENTERED=1

Dialog>Dialog1
object Dialog1: TForm
  Left = 247
  Top = 96
  HelpContext = 5000
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsSingle
  Caption = 'CustomDialog'
  ClientHeight = 278
  ClientWidth = 624
  Color = clBtnFace
  Font.Charset = ANSI_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Courier New'
  Font.Style = []
  OldCreateOrder = True
  Position = poDefault
  ShowHint = False
  OnTaskBar = True
  PixelsPerInch = 96
  TextHeight = 14
  object Label1: TLabel
    Left = 8
    Top = 8
    Width = 84
    Height = 14
    Caption = 'Text Capture'
  end
  object MSMemo1: tMSMemo
    Left = 8
    Top = 24
    Width = 609
    Height = 201
    WantTabs = True
    WordWrap = True
    ScrollBars = ssVertical
    ScrollBarsAutoShowing = ssVertical
    TabOrder = 0
  end
  object MSButton1: tMSButton
    Left = 8
    Top = 232
    Width = 153
    Height = 41
    Caption = 'Save 2 Clipboard'#13#10'With Date / Time'
    TabOrder = 1
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object MSButton2: tMSButton
    Left = 168
    Top = 232
    Width = 153
    Height = 41
    Caption = 'Save 2 Clipboard'#13#10'Without Date / Time'
    TabOrder = 2
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object MSButton3: tMSButton
    Left = 328
    Top = 232
    Width = 153
    Height = 41
    Caption = 'Get From'#13#10'Clipboard'
    TabOrder = 3
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object MSButton4: tMSButton
    Left = 552
    Top = 232
    Width = 65
    Height = 41
    Caption = 'Clear'
    TabOrder = 4
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,MSButton1,OnClick,Put1
AddDialogHandler>Dialog1,MSButton2,OnClick,Put2
AddDialogHandler>Dialog1,MSButton3,OnClick,Get
AddDialogHandler>Dialog1,MSButton4,OnClick,Clear
SetDialogProperty>Dialog1,,Caption,%APP_TITLE%
SetDialogProperty>Dialog1,,OnTaskBar,True
SetDialogProperty>Dialog1,Label1,Caption,%APP_TITLE%
GetScreenRes>Width,Height
SetDialogProperty>Dialog1,,Top,10
SetDialogProperty>Dialog1,,Width,640
Let>Width=%Width%-640
SetDialogProperty>Dialog1,,Left,%Width%

Show>Dialog1,Dialog1Result

If>Dialog1Result=2,Exit


SRT>Put1
  GetDialogProperty>Dialog1,MSMemo1,Text,MemoBox
  Day>TheDay
  Month>TheMonth
  Year>TheYear
  Hour>TheHour
  Min>TheMin
  Let>SEP=------------------------------
  PutClipBoard>Date: %TheDay%/%TheMonth%/%TheYear% - Time: %TheHour%:%TheMin%%CRLF%%SEP%%CRLF%%MemoBox%%CRLF%%SEP%%CRLF%User ID: %USER_NAME%
END>Put1

SRT>Put2
  GetDialogProperty>Dialog1,MSMemo1,Text,MemoBox
  PutClipBoard>%MemoBox%
END>Put2

SRT>Get
  GetClipBoard>FromClipBoard
  SetDialogProperty>Dialog1,MSMemo1,Text,%FromClipBoard%
END>Get

SRT>Clear
  SetDialogProperty>Dialog1,MSMemo1,Text,
  Ask>Do You Want To Clear The Clipboard As Well?,AskResult
  If>AskResult=YES
    PutClipBoard>
  EndIf
END>Clear

Label>Exit
FIREFIGHTER

hagchr
Automation Wizard
Posts: 328
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: String Replace Issues

Post by hagchr » Tue Jun 10, 2014 11:07 am

Hi, RegEx is usually very good for these kind of things. I tried to make a quick one that can hopefully help you.

//Read your raw file into variable
ReadFile>C:\...\test.txt,strFile

//Remove multiple rows and spaces
RegEx>(?m)^[^\w]+,strFile,0,M,NM,1,%CRLF%,strFile
RegEx> +,strFile,0,M,NM,1, ,strFile

MessageModal>strFile

  View Snippet Page

hagchr
Automation Wizard
Posts: 328
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: String Replace Issues

Post by hagchr » Tue Jun 10, 2014 11:38 am

I adjusted the first RegEx slightly (the previous version would also remove any initial "non-letter", "non-number" characters of a line which is probably ok most of the time. However, if you happen to have a row that starts with a quotation mark then that character would be removed which I guess is not what you want)

//Read your raw file into variable
ReadFile>C:\...\test.txt,strFile

//Remove multiple rows and spaces
RegEx>(?m)^[ \r\n]+,strFile,0,M,NM,1,%CRLF%,strFile
RegEx> +,strFile,0,M,NM,1, ,strFile

MessageModal>strFile

  View Snippet Page

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: String Replace Issues

Post by JRL » Tue Jun 10, 2014 3:51 pm

Here's my contribution using StringReplace> in a subroutine

Code: Select all

// COMPILE_OPTS|C:\_tools\MJ\Text Capture.exe|M:\Icons\fonts.ico|CONSOLE=0|INCLUDES=1| /LOGFILE=\dev\nul /NOSYSTRAY /HIDE|RUNTIMES=1|BMPS=1
Let>APP_TITLE=Text Capture
Let>MSG_HEIGHT=320
Let>MSG_WIDTH=500
Let>MSG_STAYONTOP=1
Let>MSG_CENTERED=1
Dialog>Dialog1
object Dialog1: TForm
  Left = 387
  Top = 158
  HelpContext = 5000
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsSingle
  Caption = 'CustomDialog'
  ClientHeight = 406
  ClientWidth = 1049
  Color = clBtnFace
  Font.Charset = ANSI_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Courier New'
  Font.Style = []
  OldCreateOrder = True
  Position = poDefault
  ShowHint = False
  OnTaskBar = True
  PixelsPerInch = 96
  TextHeight = 14
  object Label1: TLabel
    Left = 8
    Top = 8
    Width = 84
    Height = 14
    Caption = 'Text Capture'
  end
  object MSMemo1: tMSMemo
    Left = 8
    Top = 24
    Width = 1033
    Height = 321
    WantTabs = True
    WordWrap = True
    ScrollBars = ssVertical
    ScrollBarsAutoShowing = ssVertical
    VertScrollBarStyles.ButtonSize = 16
    HorzScrollBarStyles.ButtonSize = 16
    TabOrder = 0
  end
  object MSButton1: tMSButton
    Left = 8
    Top = 352
    Width = 153
    Height = 41
    Caption = 'Save 2 Clipboard'#13#10'With Date / Time'
    TabOrder = 1
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object MSButton2: tMSButton
    Left = 168
    Top = 352
    Width = 153
    Height = 41
    Caption = 'Save 2 Clipboard'#13#10'Without Date / Time'
    TabOrder = 2
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object MSButton3: tMSButton
    Left = 328
    Top = 352
    Width = 153
    Height = 41
    Caption = 'Get From'#13#10'Clipboard'
    TabOrder = 3
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object MSButton4: tMSButton
    Left = 552
    Top = 352
    Width = 65
    Height = 41
    Caption = 'Clear'
    TabOrder = 4
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1
AddDialogHandler>Dialog1,MSButton1,OnClick,Put1
AddDialogHandler>Dialog1,MSButton2,OnClick,Put2
AddDialogHandler>Dialog1,MSButton3,OnClick,Get
AddDialogHandler>Dialog1,MSButton4,OnClick,Clear
SetDialogProperty>Dialog1,,Caption,%APP_TITLE%
SetDialogProperty>Dialog1,,OnTaskBar,True
SetDialogProperty>Dialog1,Label1,Caption,%APP_TITLE%
GetScreenRes>Width,Height
SetDialogProperty>Dialog1,,Top,10
SetDialogProperty>Dialog1,,Width,640
Let>Width=%Width%-640
SetDialogProperty>Dialog1,,Left,%Width%

Show>Dialog1,Dialog1Result

If>Dialog1Result=2,Exit

SRT>Put1
  GetDialogProperty>Dialog1,MSMemo1,Text,MemoBox
  Day>TheDay
  Month>TheMonth
  Year>TheYear
  Hour>TheHour
  Min>TheMin
  Let>SEP=------------------------------
  PutClipBoard>Date: %TheDay%/%TheMonth%/%TheYear% - Time: %TheHour%:%TheMin%%CRLF%%SEP%%CRLF%%MemoBox%%CRLF%%SEP%%CRLF%User ID: %USER_NAME%
END>Put1
SRT>Put2
  GetDialogProperty>Dialog1,MSMemo1,Text,MemoBox
  PutClipBoard>%MemoBox%
END>Put2
SRT>Get
  GetClipBoard>FromClipBoard
  //Usage:
  //GoSub>Cleanup,{"ReturnVariableName"}OriginalString,StringToReplace,ReplaceWithString
  GoSub>Cleanup,{"FromClipBoard"},FromClipBoard,%space%%space%,space
  GoSub>Cleanup,{"FromClipBoard"},FromClipBoard,%crlf%%space%%crlf%,crlf
  GoSub>Cleanup,{"FromClipBoard"},FromClipBoard,%crlf%%space%,crlf
  GoSub>Cleanup,{"FromClipBoard"},FromClipBoard,%space%%crlf%,crlf
  GoSub>Cleanup,{"FromClipBoard"},FromClipBoard,%crlf%%crlf%,crlf
  SetDialogProperty>Dialog1,MSMemo1,Text,FromClipBoard
END>Get

SRT>Clear
  SetDialogProperty>Dialog1,MSMemo1,Text,
  Ask>Do You Want To Clear The Clipboard As Well?,AskResult
  If>AskResult=YES
    PutClipBoard>
  EndIf
END>Clear

SRT>Cleanup
  Let>CUOriginal=%CleanUp_var_1%

  Label>StartCleanup
  StringReplace>CleanUp_var_2,%CleanUp_var_3%,CleanUp_var_4,CleanUp_var_2

  If>CUOriginal=Cleanup_var_2
    Goto>CleanupComplete
  Else
    Let>CUOriginal=Cleanup_var_2
    GoTo>StartCleanup
  EndIf

  Label>CleanupComplete
  Let>%Cleanup_var_1%=Cleanup_var_2
END>Cleanup

Label>Exit

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Re: String Replace Issues

Post by CyberCitizen » Wed Jun 11, 2014 12:18 am

JRL wrote:Here's my contribution using StringReplace> in a subroutine
Thanks JRL, close however that removes all lines, I am wanting to keep single spacing anything that has double spacing or more gets converted to single spacing.

Image

Currently testing the RegEx
FIREFIGHTER

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Re: String Replace Issues

Post by CyberCitizen » Wed Jun 11, 2014 12:36 am

hagchr wrote:I adjusted the first RegEx slightly (the previous version would also remove any initial "non-letter", "non-number" characters of a line which is probably ok most of the time. However, if you happen to have a row that starts with a quotation mark then that character would be removed which I guess is not what you want)
Thanks for this, this works as I was wanting. Just wondering why I couldn't do it with string replace. Will have to play a bit more. Thank you.
FIREFIGHTER

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Re: String Replace Issues

Post by CyberCitizen » Wed Jun 11, 2014 2:00 am

Sorry for the triple post. Is it possible for one more line of code to remove the trailing carriage return. Eg when copying something from email I normally end up with a new line at the end of it. Is there a way to use RegEx to remove this trailing line as well?

I am really not good with RegEx, still learning as I go. This is why your code has been useful for me.
FIREFIGHTER

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: String Replace Issues

Post by JRL » Wed Jun 11, 2014 4:00 am

I understand that you really want a nice tidy regex solution but like you I'm crap at regex. In the mean time, Replace

Code: Select all

GoSub>Cleanup,{"FromClipBoard"},FromClipBoard,%crlf%%crlf%,crlf
With

Code: Select all

GoSub>Cleanup,{"FromClipBoard"},FromClipBoard,%crlf%%crlf%%crlf%,%crlf%%crlf%

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Re: String Replace Issues

Post by CyberCitizen » Wed Jun 11, 2014 5:18 am

JRL wrote:I understand that you really want a nice tidy regex solution but like you I'm crap at regex. In the mean time, Replace

Code: Select all

GoSub>Cleanup,{"FromClipBoard"},FromClipBoard,%crlf%%crlf%,crlf
With

Code: Select all

GoSub>Cleanup,{"FromClipBoard"},FromClipBoard,%crlf%%crlf%%crlf%,%crlf%%crlf%
Just wondering why I couldn't just use something like
StringReplace>SOURCE,%crlf%%crlf%,%crlf%,SOURCE
FIREFIGHTER

hagchr
Automation Wizard
Posts: 328
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: String Replace Issues

Post by hagchr » Wed Jun 11, 2014 9:20 am

Just for completeness. To remove any trailing spaces or line breaks you can add one more RegEx line. I also changed the first line to avoid some special cases potentially breaking it.

//Read your raw file into variable
ReadFile>C:\...\test.txt,strFile

//Remove multiple rows and spaces
RegEx>(?<=[\n])\s+[\n],strFile,0,M,NM,1,%CRLF%,strFile
RegEx> +,strFile,0,M,NM,1, ,strFile
RegEx>\s+\Z,strFile,0,M,NM,1,,strFile

MessageModal>strFile

  View Snippet Page

Also, you did not mention, but in case you also want to remove any inital spaces or TABs on any of the "text rows" you could also add this line:
RegEx>(?m)^[ \t]+,strFile,0,M,NM,1,,strFile
  View Snippet Page
As you have seen in JRL's email you can solve this in different ways. If you want to look into RegEx then hopefully this helps. Let me know if you need any more explanations

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: String Replace Issues

Post by JRL » Wed Jun 11, 2014 2:26 pm

CyberCitizen wrote:Just wondering why I couldn't just use something like
StringReplace>SOURCE,%crlf%%crlf%,%crlf%,SOURCE

Code: Select all

//Suppose you were converting pairs of CRLF to X

Let>text1=Line one%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%Line two
StringReplace>text1,%crlf%%crlf%,X,text2


//StringReplace matches the %crlf% pairs as it runs into them.
//It finds the first two and converts them to the "replace with" string "X"
//Then it finds the next two and converts them and so on.
//
//%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%
//     \/          \/          \/          \/
//      X           X           X           X
//
//Four pairs of "crlf" would be replaced with four Xs
//Line oneXXXXLine two


//The same thing happens if the "replace with" string is another "crlf". 
//
//%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%
//     \/          \/          \/          \/
//   %crlf%      %crlf%      %crlf%      %crlf%

//If you want the function to be recursive,  you have to write the code for that.
//that's what the "Cleanup" subroutine does in the script I posted previously. The
//subroutine compares the text string before the StringReplace with the text string
//after the StringReplace.  If the text strings are different,  the subroutine repeats
//and runs the StringReplace again. If the text strings are identical, then StringReplace
//did not find any matches and the subroutine ends.


Let>text1=Line one%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%Line two
GoSub>Cleanup,{"text2"},text1,%crlf%%crlf%,crlf

//So the result you get using the "Cleanup" subroutine is:
//
//%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%%crlf%
//     \/          \/          \/          \/
//   %crlf%      %crlf%      %crlf%      %crlf%         (First pass)
//           \/                      \/
//         %crlf%                  %crlf%               (Second pass)
//                       \/
//                     %crlf%                           (Third pass)
//                       \/
//                     %crlf%                           (Fourth pass)
//                                                      No change so the sub completes
//
//The resulting string would look like:
//Line one%crlf%Line two



SRT>Cleanup
  Let>CUOriginal=%CleanUp_var_1%
  Label>StartCleanup
  StringReplace>CleanUp_var_2,%CleanUp_var_3%,CleanUp_var_4,CleanUp_var_2

  If>CUOriginal=Cleanup_var_2
    Goto>CleanupComplete
  Else
    Let>CUOriginal=Cleanup_var_2
    GoTo>StartCleanup
  EndIf

  Label>CleanupComplete
  Let>%Cleanup_var_1%=Cleanup_var_2
END>Cleanup

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Re: String Replace Issues

Post by CyberCitizen » Thu Jun 12, 2014 3:51 am

Just want to say thank you to the both of you. You have helped me out with my script as well as given me some awesome tips / learning to do.
FIREFIGHTER

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts