Regex replace `a.b` to `a. b`? [duplicate]

2024/10/5 16:29:39

I am trying to change all strings of the type a.b to a. b. It should also work if any of the characters are capital case. Note that a. should not be changed to a.\(space).

I have tried using regex to do this like so:

a = "a.b"
a = re.sub(r'[a-b]\.[a-b]', ' .', a)

The output should be a. b, but it is ., because it replaces all the characters as well, instead of keeping the characters and just putting a space after the dot.

Does anyone know how to do this in Python?

Edit: I am trying to add a space when there is a period in between two sentences. However, I can't use str.replace, since it would add a space in this case also, like 1.5 becoming 1. 5, which is not what I want. I tried to do regex, but wasn't able to get it to work.

Also, this has to work in all possible sentences, not just a.b

Answer

You can use the following ([abAB])\.([abAB])

>>> import re
>>> s = ['a.b', 'A.b', 'a.B', 'A.B']
>>> [re.sub(r'([abAB])\.([abAB])', r'\1. \2', i) for i in s]
['a. b', 'A. b', 'a. B', 'A. B']

The issue with your approach is that you are not saving capturing groups to use in the result of the substitution.

If you want this to work for the entire alphabet, you can use this:

>>> s = 'billy.bob'
>>> re.sub(r'([a-zA-Z])\.([a-zA-Z])', r'\1. \2', s)
'billy. bob'
https://en.xdnf.cn/q/119829.html

Related Q&A

Is there something wrong with this line of Python code?

I tried this line of code, and it kept giving me the SyntaxError.print(/ / - / \ / | * 30, end=\r)^It pointed on the brackets. Any suggestions? Thanks!

TypeError: tuple indices must be integers or slices, not str postgres/python

Hi Im trying to get a timestamp from a row called time in my postgres database, however Im getting the error. Its the second row in the database. The error TypeError: tuple indices must be integers or …

Pandas Python: KeyError Date

I am import into python where it will automatically create a date time object.However I want the first column to be a datetime object in Python. Data looks likeDate,cost 41330.66667,100 41331.66667,101…

How to resample 1 minute data into 15 minute data?

CSV file. df before resample and after applying: df["dateandtime"] = (pd.to_datetime(df.pop("DATE").str.cat(df.pop("TIME"), sep=" "))) df = df.set_index(pd.Datet…

Python Pygame Lighting for Pong

Hey Guys Im writing a little Pong-Game in Pygame and wanted to use a glowing-effect on the Ball and the Bats. But Pygame dosent support this effects and make solid blocks out of it. Is there a way to h…

python - List.remove method not applicable as the function with the map builtin?

Question Can List.remove not be used as the function to apply in the map(function, iterable, ...) builtin? %%timeit import random m = _max = 10000 n = random.randint(1, m)outer = random.sample(populat…

How do I change a sum for string for it to work?

This is my whole program Im working on (with the function not in the right place due to the code needing to be indented) but anyway there is a problem that Im not sure how to fix.How do I change it so …

Convert type str to numerator/ denominator

Im having some trouble converting type str to numbers. I use a separate text-file containing the following numbers 1, 2, 3, 4, 5, 6 and then I import these numbers into python and save them as a list. …

discord.py MemberNotFound exception when passing a real member

When I pass *grant @user add in Discord I get the following exception: Ignoring exception in command grant: Traceback (most recent call last):File "/Users/test/PycharmProjects/slapdash/venv/lib/py…

using argument end= for print function in Python 3.3

Consider the following code:i=0 while i<5:print(i, end=" ")i = i + 1which results in the output:0 1 2 3 4if i want to add a string "the result" before 1 2 3 4, output expected: t…