topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Saturday December 14, 2024, 5:31 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Search files that contain trailing spaces  (Read 6221 times)

blackcat

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 62
    • View Profile
    • Donate to Member
Search files that contain trailing spaces
« on: April 23, 2009, 07:18 AM »
Is this possible? Can we search for all text files that contain trailing spaces and batch remove those trailing space in all files? Yea using regex, if that is the only way to do it. And also regex rules to replace those spaces. I think im going to use "Find in files" feature in notepad++  :D

Any idea or suggestions?

tranglos

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,081
    • View Profile
    • Donate to Member
Re: Search files that contain trailing spaces
« Reply #1 on: April 23, 2009, 08:03 AM »
The search regex would be something like
[ ]+$

(the square brackets aren't required, but help visualize the space character).

Tested in Total Commander, but should work for any regular expression engine. If you need to account for tabs as well, in TC you could use
\s+$

TC does not support a whitespace special character, but many regex search engines do. In TextPad, for example, you could search for
[:blank:]

The metod of stripping the trailing blanks though will depend on the particular program and its implementation of regular expressions. Usually the help file will have examples of search and replace operations. (I use searching heavily, but almost never do automated replace, since it can wreck a week's worth of my work in two seconds. I'm sure others will post with more suggestions.)

And if the number of files is small, I would simply use a text editor with an option to strip trailing blanks when saving (e.g. TextPad), open all the files at once and save them. For up to a dozen files or so, that would be faster than reading the manual on regexes :)

« Last Edit: April 23, 2009, 08:05 AM by tranglos »

blackcat

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 62
    • View Profile
    • Donate to Member
Re: Search files that contain trailing spaces
« Reply #2 on: April 23, 2009, 08:45 AM »
Very helpful tips tranglos

So \s+$ includes both trailing spaces and tabs, right?

What's the different with [ \t]+$
« Last Edit: April 23, 2009, 08:46 AM by blackcat »

tranglos

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,081
    • View Profile
    • Donate to Member
Re: Search files that contain trailing spaces
« Reply #3 on: April 23, 2009, 01:32 PM »
So \s+$ includes both trailing spaces and tabs, right?

That's right. It's the same as [:blank:]+$ - but this latter syntax, the so called POSIX notation, is not always supported.


What's the different with [ \t]+$

No difference again, although \s would theoretically match any other blank characters, if they existed :)

The real difference is in what a particular application supports. I've just found out for example that TotalCommander supports \s, but for some reason does not seem to understand \t - even though the help file says it does. I'll try again and report a bug if confirmed.

Another area where apps differ is in the interpretation of line breaks. In TextPad, a linebreak is \n, so the regex could also be written as
[ \t]+\n

and you would replace that with
\n

In other text editors though \n may only mean the newline character. As you may know, in DOS and Windows the linebreak actually consists of two characters: caret return and new line. So some apps require you to spell them out:
[ \t]+\r\n

and you would replace that with
\r\n