0

I want to eliminate white space between 2 words among sentence containing many words

My code looks like this:

import re
sentence = "open app store"
pattern = re.compile(r'\b([a-z]) (?=[a-z]\b)', re.I)
sentence = re.sub(pattern, r'\g<1>', sentence)
print(sentence)

output:

open app store

I want to remove white space between app and store. I want output like this "open appstore".

Note that app won't always come up with store, app can come along with some other word after it, e.g. app maker.

5
  • 6
    What rule are you applying so that it becomes open appstore and not openapp store ? Commented Oct 5, 2017 at 12:19
  • Do you want to remove the whitespace before the last word in a string? See ideone.com/uYTWnZ Commented Oct 5, 2017 at 12:20
  • What is your rule? If app store may come anywhere in the string, how do you want to match it? Commented Oct 8, 2017 at 21:34
  • @WiktorStribiżew: app store can come anywhere in sentence. I want to match app store string in sentence and replace it with appstore. Commented Oct 9, 2017 at 4:48
  • See my answer explaining the problem and 2 solutions. Commented Oct 9, 2017 at 14:15

3 Answers 3

1

Let's have a look at your pattern: it matches a word boundary, then captures any ASCII letter into Group 1, then matches a space, and then asserts there is a single ASCII letter followed with a word boundary. So, it can match a b in My a b string, but not the app store.

Now, it seems your app value is static, after it you want to match 1 or more whitespaces only if there is another word following app. You may follow two strategies.

You may match app that is followed with whitespace(s) and a letter and then remove the whitespaces (see this Python demo):

re.sub(r"\b(app)\s+([a-z])", r"\1\2", sentence, flags=re.I)

(also, see the regex demo) or you may use the known words that follow app and only remove the spaces between them:

re.sub(r"\b(app)\s+(store|maker|market|etc)", r"\1\2", sentence, flags=re.I)

See another regex demo and another Python demo.

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

Comments

1

This could work for you.

>>> import re
>>> sentence = "this is an open app store and this is another open app store."
>>> pattern = re.compile(r'app[\s]store')
>>> replacement = 'appstore'
>>> result = re.sub(pattern, replacement, sentence)
>>> result
'this is an open appstore and this is another open appstore.'

Edit: You could use this function to eliminate whitespace(s) between any two words.

import re

def remove_spaces(text, word_one, word_two):
    """ Return text after removing whitespace(s) between two specific words.

    >>> remove_spaces("an app store app maker app    store", "app", "store")
    'an appstore, app maker, appstore'
    """

    pattern = re.compile(r'{}[\s]*{}'.format(word_one, word_two))    # zero or more spaces
    replacement = word_one + word_two
    result = re.sub(pattern, replacement, text)

    return result

2 Comments

"app" won't always come up with "store". "app" can come along with some other extension. e.g. "app maker". Can you please help me accordingly?
@Sonal, it still works. It seems you didn't test it with a different word after "app "
-1

Try This :

import re
sentence = "This is test"
pattern = re.compile(r'(.*)\b\s+(?=[a-z])', re.I | re.S)
sentence = re.sub(pattern, r'\1', sentence)
print(sentence)

Output : This istest

hope it works for you.

1 Comment

"app store" won't always be the last word of sentence. It can come anywhere in sentence.

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.