-2

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

hockeybro
  • 981
  • 1
  • 13
  • 41
  • `a.replace('.', '. ')`? Not really sure how it couldn't be `a. \s`. – pstatix Apr 26 '18 at 03:31
  • The reason replace wouldn't work is because it would add a space at the end of a sentence, when there is not another one that follows. – hockeybro Apr 26 '18 at 03:33
  • @Isaac this doesn't appear to be a duplicate of that. – Adam Smith Apr 26 '18 at 03:34
  • @AdamSmith does to me – Isaac Apr 26 '18 at 03:35
  • @Isaac please check out my edit. It is a regex question, which is why I tried `re.sub`, but as you can see I wasn't able to do it. – hockeybro Apr 26 '18 at 03:39
  • Give examples as in what other possible sentences and what you want to change them to. Btw, have you actually read the dupe post? It's exactly the same problem. – Taku Apr 26 '18 at 03:41
  • What does `a.b` mean? Does this mean any character from a-z? Does it have to be exactly one character each or can it be multiple characters each like `apple.bee`? Is this limited to only a and b like so `aaa.bbbbbbb`? Or are these variable representatives for numbers like `1.2` and `34.56`? – Ultimater Apr 26 '18 at 03:50
  • @Ultimater yes, it should work for multiple characters each. And numbers like `34.56` should stay at `34.56`. – hockeybro Apr 26 '18 at 04:24
  • What characters are allowed? Only a and b? Or the entire alphabet? – Ultimater Apr 26 '18 at 04:28
  • 1
    The entire alphabet. – hockeybro Apr 26 '18 at 04:28

1 Answers1

1

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'
user3483203
  • 50,081
  • 9
  • 65
  • 94