Home > Software design >  How to remove whitespaces in a string except from between certain elements
How to remove whitespaces in a string except from between certain elements

Time:12-05

I have a string similar to (the below one is simplified):

"  word=       {his or her}      whatever  "

I want to delete every whitespace except between {}, so that my modified string will be:

"word={his or her}whatever"

lstrip or rstrip doesn't work of course. If I delete all whitespaces the whitespaces between {} are deleted as well. I tried to look up solutions for limiting the replace function to certain areas but even if I found out it I haven't been able to implement it. There are some stuff with regex (I am not sure if they are relevant here) but I haven't been able to understand them.

EDIT: If I wanted to except the area between, say {} and "", that is:

if I wanted to turn this string:

"  word=       {his or her} and "his or her"      whatever  "

into this:

"word={his or her}and"his or her"whatever"

What would I change

re.sub(r'\s (?![^{]*})', '', list_name) into?

CodePudding user response:

To solve this problem, you can use regular expressions to find and replace the whitespace characters. In particular, you can use the re.sub function to search for whitespace characters outside of the curly braces and replace them with an empty string.

Here is an example of how you can use re.sub to solve this problem:

import re

# Define the input string
input_str = " word= {his or her} whatever "

# Use a regular expression to search for whitespace characters outside of the curly braces
output_str = re.sub(r'\s (?![^{]*})', '', input_str)

# Print the result
print(output_str)

This code will print the modified string as follows:

word={his or her}whatever

The regular expression r'\s (?![^{]*})' matches the whitespace that you want to remove from the string. The negative lookahead assertion ensures that the match is not followed by a string of the form {...}, so that the whitespace between the curly brackets is not removed. The `re.sub function replaces these matches with an empty string, effectively removing the whitespace characters from the input string.

You can use this approach to modify your string and remove the whitespace characters outside of the curly braces.

CodePudding user response:

See instead going arround re you can replace uisng string.replace. Which will be much more easier and less complex when you playing around strings. Espacillay when you have multiple substitutions you end up bigger regex.

st ="  word=       {his or her}      whatever  "
st2="""  word=       {his or her} and "his or her"      whatever  """

new = " ".join(st2.split())
new = new.replace("= ", "=").replace("} ", "}").replace('" ' , '"').replace(' "' , '"')
print(new)

Some outputs

Example 1 output

word={his or her}whatever

Example 2 output

word={his or her}and"his or her"whatever

CodePudding user response:

You can use by replace

def remove(string):
    return string.replace(" ", "")

string = 'hell o whatever'
print(remove(string)) // Output: hellowhatever
  • Related