Aditya Kuls Aditya Kuls - 4 months ago 7
Python Question

How to split a statement based on dots ('.'), while excluding dots inside angular brackets(< . >) using regular expressions in python?

I want to split statements by dots using regex in python, while excluding certain dots inside the angular brackets.
eg:
Original Statement :

'my name 54. is <not23.> worth mentioning. ok?'


I want to split it into following sentences:

Statement 1 : 'my name 54'
Statement 2 : ' is <not23.> worth mentioning'
Statement 3 : ' ok'

Answer

Split on the following regex:

\.(?![^<]*>)

Live demo

import re

str = 'my name 54. is <not23.> worth mentioning. ok?'
regex = re.compile(r"\.(?![^<]*>)")
arr = regex.split(str)

print(arr)