Question: I want to write a regex in Python that does the following

Convert all sentences that end with `[alphanumeric]\n` and replace it with "."

So for example

```
I went there
I also went there.
It is

That-
I too went there!
It went there?
It is 3

```

so let's say we have

Should be converted to 

```
I went there.
I also went there.
It is.

That-
I too went there!
It went there?
It is 3.

```

How can I do this?

EDIT: The input string is

```
s = "I went there\nI also went there.\nIt is\n\nThat-\nI too went there!\nIt went there?\nIt is 3"

```

Also, "?" should not be appended with a "."

EDIT2: I modified the example so it contains a double `\n` and a sentence ending with `-`. So "-" should not be appended with a "."

Answer: 
Try something like this:

```
s = 'I went there'

if s[-1] not in ['!', ',', '.', '\n']:
    s += '.'

```

**Edit:**

With your new input, the following should work:

```
new_string = ''.join('{}.\n'.format(item) if (item and item[-1] not in '!?.,-') else '{}\n'.format(item) for item in s.split('\n'))

```

If you do not want `\n` at the end of `new_string`, you can remove it:

```
new_string = new_string.rstrip('\n')

```

**Output:**

```
>>> s = "I went there\nI also went there.\nIt is\n\nThat-\nI too went there!\nIt went there?\nIt is 3"
>>> new_string = ''.join('{}.\n'.format(item) if (item and item[-1] not in '!?.,-') else '{}\n'.format(item) for item in s.split('\n'))
>>>
>>> print(new_string)
I went there.
I also went there.
It is.

That-
I too went there!
It went there?
It is 3.

```
Simple solution using `re.sub()` function and specific regex pattern:

```
s = "I went there\nI also went there.\nIt is\n\nThat-\nI too went there!\nIt went there?\nIt is 3"
s = re.sub(r'(?<=[^,.!?-\s])(\n|$)', r'.\1', s, re.M)
print(s)

```

The output:

```
I went there.
I also went there.
It is.

That-
I too went there!
It went there?
It is 3.

```

---

`(\n|$)` - matches a linebreak(new line) or the end of the string

`(?<=[^,.!?-\s])` - ensures that there's no specific characters before the above mentioned match

`\1` - refers to the first capturing group which is `(\n|$)`