Replacing a number of tabs with a number \t\t\t with 3

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
D3s7
Posts: 1
Joined: Fri Nov 21, 2003 2:28 am

Replacing a number of tabs with a number \t\t\t with 3

Post by D3s7 »

I want to search my document for tabs and replace them with a function to create tabs

EX: if I have \t\t\t (3 tabs) i want to replace those 3 with MakeTab(3)

with a regexpresion, is there a way to do that??
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

Hello D3s7:
The answer is just as you wrote in your question:

Here is a sample of text where I replaced three consecutive tabs on the first three lines. For demo purposes here I have included the word "tab" to represent the actual \t code in the document.
one tab two tab tab three MakeTab(3) four tab tab tab tab
one tab two tab tab three MakeTab(3) four tab tab tab tab
one tab two tab tab three MakeTab(3) four tab tab tab tab
one tab two tab tab three tab tab tab four tab tab tab tab
one tab two tab tab three tab tab tab four tab tab tab tab
one tab two tab tab three tab tab tab four tab tab tab tab
The Sensible Solution:
(Ignore following "double quotes"
Search RegEx for "\t\t\t"
Replace with "MakeTab(3)"

But this was too easy, so I must have misunderstood your question.

If you already have the tabs, why do you need a function to make them?

How is that function going to be executed or called? Is MakeTab(3) an existing function in another program that will read this file?

Are you looking for TextPad to create and execute a function "MakeTab(n)" to make "n" tabs? Like looking to Search for something else, and replace it with \t\t\t?
Hope this was helpful.............good luck,
Bob
User avatar
talleyrand
Posts: 624
Joined: Mon Jul 21, 2003 6:56 pm
Location: Kansas City, MO, USA
Contact:

Post by talleyrand »

I believe you are asking for something to count the number of successive tabs and then replace all of those tabs with a call to a function specifying the number of tabs replaced, no? I'll defer to Bob or s_reynisson for a regular expression solution. If you'd like a Python solution, I just might happen to have one. :roll:

Convert this
\t\ttwoTabs
\tonetab
\tone\tand one\t\tand two
\t\t\tthree tabs

to this
MakeTab(2) twoTabs
MakeTab(1) onetab
MakeTab(1) one MakeTab(1) and one MakeTab(2) and two
MakeTab(3) three tabs

Install Python and you're good to go.

Copy this code and paste into Textpad.
Save it to something like C:\D3s7.py
Update the filename variable

C:\>python D3s7.py

You can also run it straight out of Texpad. Set the command to wherever you installed Python (probably C:\python23\python.exe)

Code: Select all

import sys

def tabReplacer(fileName, functionName):
   """
   Look through a file and for each line count the occurences
   of the tab character.  Replace those characters with a call to
   functionName passing the specified count as an argument.
   Returns a string containing the updated data
   """
   #out will be a big string to contain what we're replacing
   out =  ''
   #read every line in the given file
   for currentLine in open(fileName, 'r').readlines():
      #This assumes the tabs will all be on the same line
      tabCount = 0
      #break the line apart to it's individual letters
      for letter in currentLine:
         #if the current letter is a tab then increment our count
         if (letter == '\t'):
            tabCount += 1
         else:
            #at this point we are not looking at a tab character so
            # if we've found any tabs,
            # we need to build the replacement string
            if (tabCount):
               #if it's not the first thing on the line and it's not
               # an empty string, then add a space for readability
               if (out[len(out)-1:] != '\n' and out):
                  out += ' '

               #actually build the replacement string
               out += functionName + '(' + str(tabCount) + ') '
               #reset our counter
               tabCount = 0
            #add whatever was the non-tab character to our output
            out += letter
   return out

def main():

   #UPDATE THIS FILENAME
   fileName = 'test_d3s7.txt'
   #create a new file for output
   output = open((fileName + '.out'), 'w')
   output.writelines(tabReplacer(fileName, 'MakeTab'))
   output.close()

if __name__ == '__main__':
   main()
I choose to fight with a sack of angry cats.
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

Hey talleyrand, take it easy on me! Don't know if I can take the pressure. :D

That programming looks great, nice to have those tools. I'm just now trying to self teach myself Perl and you throw that Python stuff in front of me as if to say forget Perl use this instead. Grrrr, decisions, decisions.

Well, I think I am committed now, but will keep looking at your Python coding for future tool expansions. Thanks for showing us the power available. :D
Last edited by Bob Hansen on Fri Nov 21, 2003 3:14 pm, edited 1 time in total.
Hope this was helpful.............good luck,
Bob
User avatar
s_reynisson
Posts: 939
Joined: Tue May 06, 2003 1:59 pm

Post by s_reynisson »

Having pulled all my hair out, esp. after talleyrand's reference,
I re-read the original post from D3s7.
It's headline say's:
Replacing a number of tabs with a number \t\t\t with 3
and in the main text it say's:
I have \t\t\t (3 tabs) i want to replace those 3 with MakeTab(3)
The answer would then be, using POSIX regular expression:
Find: \t\t\t
Replace: 3 (or MakeTab(3))
Repeat as needed for 2 and 1, 1 last of course.

Kudos to talleyrand and Python, again!
(btw, my soon to be new signature:
"When will Python support proper handling of codepages?:roll:")
Post Reply