Jump to content
Sign in to follow this  
Coded4Decades

deleting lines of code with SciTE Edit Find and Replace

Recommended Posts

Coded4Decades

How can I delete lines using Find and replace?

Here are two examples of what I want to do.

Example 1: 

    To improve readability, I often put 5 or more blank lines before and after chunks of particulary difficult code.

    Before I put the program into production, I want replace 2 or more consecutive blank lines with a single blank line.

    How do I do this?

   

   

Example 2: To debug a program I inserted hundreds of debugging lines. The program now looks like this

$a = $b

$x = $y

Logwrite ("debug info" & $x)

$c = $d

Logwrite ("debug info" & other junk)

etc

etc for about 1000 lines.

The program now works, so I want to remove the LogWrite lines.

I tried using find and replace with regular expressions to DELETE the lines with this pattern.

    ^logwrite(.*$

But, that only BLANKED the lines, it did not delete them.

I also failed with variations of ^logwrite(.*[n]$

And, I tried "transform backslash characters" but that did not help.

Help !

Bob

Share this post


Link to post
Share on other sites
BrewManNH

SciTE's find and find & replace can use a RegEx in the search parameters and references in the replace parameters.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Melba23

Coded4Decades,

I have never found SciTE's RegEx replace to be very good - something their developers could improve a lot in my opinion. :(

I would just write another script to do the job for you - that is what Autoit is good at. ;)

$sScript = FileRead("OrgScript.au3")
$sNewScript = StringRegExpReplace($sScript, "Logwrite.*($|\R)", "")
FileWrite("NewScript.au3", $sNewScript)
Something along those lines works for me. :)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______Open Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
BrewManNH

I just reread the Regex information for SciTE and it wouldn't work anyways, as it can't find CR or LF because it only searches line by line and not the whole text.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Jos

The n is not supported in SciTE as indicated in the FAQ.

Only option I can think of is to do it in 2 steps:

  1. Replace the text in your regex with something static/unique:post-19-0-90501400-1395494706_thumb.png
  2. Replace the new Static text with Transform selected: post-19-0-20361300-1395494751_thumb.png

 

Cheers

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
BrewManNH

This LUA script will replace multiple blank lines with a single blank line and it will leave single blank lines as-is.

function del_empty_lines()
  local txt = editor:GetText()
  if #txt == 0 then return end
  local chg, n = false
  while true do
    txt, n = string.gsub(txt, "(\r?\n\r?\n)%s*\r?\n", "%1")
    if n == 0 then break end
    chg = true
  end
  if chg then
    editor:SetText(txt)
    editor:GotoPos(0)
  end
end

I added this to the Tools.lua file in SciTE, and added the following to the SciteUser.properties file so I can call it using a right click menu in SciTE.

# 46 Remove Empty Lines
# command.name.46.*=Remove Empty Lines
command.46.*=del_empty_lines
command.subsystem.46.*=3
user.context.menu=||Add as Snippet|1116|Remove Empty Lines|1146|

You can uncomment the second line of the above to add the tool to the Tools menu in Scite, and/or comment out/remove the last line to take it off of the right click menu.

 

The LUA script was found here.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
JohnOne

I done the same as the OP a few years ago, as soon as I had, my script went to crap and I needed them back.

Next time I just left them and replaced "Mydebugfunc(...)" with "; Mydebugfunc(...)" to comment them out.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
Coded4Decades

all good suggestions.  The one I use was Jos's two step approach which worked great for Example #2.

For example #1 I only needed a one step to change all groups of consecutive blank lines into a single blank line:

 I simply changed rnrnrn  to rnrn  and repeated the find until there were no more replacements.

Thank for your help, that was driving me crazy.

Edited by Coded4Decades

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  

×