# Insert space into string, using Regex

**URL:** <https://discuss.python.org/t/insert-space-into-string-using-regex/38919>\
**Category:** Python Help\
**Tags:** help\
**Created:** [November 16, 2023, 9:16pm UTC](https://discuss.python.org/t/insert-space-into-string-using-regex/38919 "2023-11-16T21:16:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marian](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@marian](https://discuss.python.org/u/marian)\
**Post date:** [November 16, 2023, 9:16pm UTC](https://discuss.python.org/t/insert-space-into-string-using-regex/38919/1 "2023-11-16T21:16:43Z")

</div>

What’s wrong in my code?

I am trying to insert space `' '` after the colon `':'` , if it is missing.

I am matching `Full Name:J`  
capturing group `Full Name:`  
replacing with `'\1 '`

But it seems the capturing group is not working properly,  
I get this distorted result: `Full Name: ohn Doe.`

Any suggestions?  
Thank you.

```auto
import re

# Example input string
input_string = "Full Name:John Doe."

# Use re.sub() to add a space after the colon
pattern = r"(Full Name:)\w"
replacement = r"\1 "

# Perform the substitution
result = re.sub(pattern, replacement, input_string)

# Print the result
print(result)

```

---

<div class="post-metadata">

**Author:** ![effigies](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/effigies/32/35835_2.png) [@effigies](https://discuss.python.org/u/effigies)\
**Post date:** [November 16, 2023, 9:30pm UTC](https://discuss.python.org/t/insert-space-into-string-using-regex/38919/2 "2023-11-16T21:30:01Z")

</div>

That looks right. You’re matching the string `Full Name:J`, capturing `Full Name:`, and so replacing the matched string (not just the captured string) with `Full Name: `.

From your description of what you want, it seems like you want to match an optional space, not a word character. That regex would look like:

```auto
pattern = r'(Full Name:) ?'

```

This would match `Full Name:` and be replaced with `Full Name: `. Or if there already is a space, then you will replace `Full Name: ` with `Full Name: `.

---

<div class="post-metadata">

**Author:** ![steven.rumbalski](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@steven.rumbalski](https://discuss.python.org/u/steven.rumbalski)\
**Post date:** [November 16, 2023, 9:35pm UTC](https://discuss.python.org/t/insert-space-into-string-using-regex/38919/3 "2023-11-16T21:35:31Z")

</div>

> [@marian](#):
>
> I am matching `Full Name:J`  
> capturing group `Full Name:`  
> replacing with `'\1 '`

Expressed with a string method, you are doing

```
input_string.replace('Full Name:J', 'Full Name: ')

```

Can you see why that would result in `Full Name: ohn Doe.`?

Update your pattern to use a _lookahead assertion_ and your code works:

```
pattern = r'(Full Name:)(?=\w)'

```

From the [docs](https://docs.python.org/3/library/re.html#regular-expression-syntax)

> `(?!...)`
> 
> Matches if `...` doesn’t match next. This is a _negative lookahead assertion_. For example, `Isaac (?!Asimov)` will match `'Isaac '` only if it’s _not_ followed by `'Asimov'`.

---

<div class="post-metadata">

**Author:** ![marian](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@marian](https://discuss.python.org/u/marian)\
**Post date:** [November 17, 2023, 2:21pm UTC](https://discuss.python.org/t/insert-space-into-string-using-regex/38919/4 "2023-11-17T14:21:40Z")

</div>

ok, I got it.  
thank you all for explanations, both options work!  
😉

---

<div class="post-metadata">

**Author:** ![steven.rumbalski](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@steven.rumbalski](https://discuss.python.org/u/steven.rumbalski)\
**Post date:** [November 17, 2023, 6:52pm UTC](https://discuss.python.org/t/insert-space-into-string-using-regex/38919/5 "2023-11-17T18:52:36Z")

</div>

Another option is to change your pattern to have two capturing groups:

```
pattern = r'(Full Name:)(\w)'

```

Then your replacement can use both groups:

```
replacement = r'\1 \2'

```
