1

I have a string as

 "This is a small \t\t world"

Assume that the string has 2 tabs in between the words "small" and "world". How can I trim one of the tab spaces so that I get:

 "This is a small \t world"

The words "small" and "world" can appear only once in the sentence. Basically given two specific words, I want to trim the extra tab between them

7
  • 4
    How do you want to identify that? Does it have to be between the words 'small' and 'world'? At the end of the string? This is ambiguous. Commented Jan 18, 2013 at 18:24
  • Please check my edit. I want to remove the tab only between the words small and world. I do not want any other tabs to be replaced Commented Jan 18, 2013 at 18:24
  • 2
    What if those words appear multiple times? What if there is other stuff in between too? What if they are in a different order? It's hard to answer a question like this with such a vague specification of the problem. Commented Jan 18, 2013 at 18:26
  • 1
    I have a nagging suspicion that you have tried to simplify what you're actually doing. Are there any other variables? For example, could there be anything between small and world besides one space character on each side and any number of tabs? Commented Jan 18, 2013 at 18:26
  • 1
    The words "small" and "world" can appear only once in the sentence. Basically given two specific words, I want to trim the extra tab between them without editing any other tabs present in the sentence Commented Jan 18, 2013 at 18:31

4 Answers 4

2

Using re...

import re

s = b"This is        a small         world"

s = re.sub(r'(.*\bsmall *)\t+( *world\b.*)', r'\1\t\2', s)

print s

Output:

>>> 
This is          a small     world

This will keep all spaces before and after the two tabs.

Sign up to request clarification or add additional context in comments.

Comments

1
def remove_tab(st, word1, word2):
    index1 = st.find(word1)
    index2 = st[index1:].find(word2)
    replacement = st[index1:index2].replace('\t\t', '\t')
    return st[:index1] + replacement + st[index2:]

Comments

1

using regex:

In [114]: def func(st,*words):
    rep=" \t ".join(words)
    reg="\b%s\s?\t{1,}\s?%s\b"%(words[0],words[1])
    return re.sub(reg,rep,st)
   .....: 

In [118]: strs='This is \t\t\t a small\t\t\tworld, very small world?'

In [119]: func(strs,"small","world")
Out[119]: 'This is \t\t\t a small \t world, very small world?'

In [120]: func(strs,"is","a")
Out[120]: 'This is \t a small\t\t\tworld, very small world?'

1 Comment

Maybe throw a \b in there? "small \t\t worldly case".
0

You can use the Python re module to use regular expressions:

import re

s = "This is \t\t a small \t\t world"

s1 = re.sub(r'(?<=small +)\t+(?= +world)', '\t', s)

This will find one or more of \t in a row between "small " and " world" and replace the entire sequence of \t's with a single \t.

3 Comments

I think you should probably make it handle extra whitespace between the small & world
I edited the code sample to account for one or more spaces characters next to small and world.
look-behind requires fixed-width pattern

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.