You need to use re.split if you want to split a string according to a regex pattern.
tokens = re.split(r'[.:]', ip)
Inside a character class | matches a literal | symbol and note that [.:] matches a dot or colon (| won't do the orring here).
So you need to remove | from the character class or otherwise it would do splitting according to the pipe character also.
or
Use string.split along with list_comprehension.
>>> ip = '192.168.0.1:8080'
>>> [j for i in ip.split(':') for j in i.split('.')]
['192', '168', '0', '1', '8080']
Answer from Avinash Raj on Stack OverflowSplit string based on RegEx in Python?
python - Split string at every position where an upper-case word starts - Stack Overflow
python - The result list contains single spaces when splitting a string with re.split("( )+") โ is there a better way? - Stack Overflow
Split String every nth char / or the first occurrence of a period
I have a fairly big regex matching various types of common mail headers which are used between replies. I'm trying to use re.split to separate each reply as follows:
r = re.compile(r'((?:^ *Original Message processed by david.+?$\\n{,7})(?:.*\\n){,3}(?:(?:^|\\n)[* ]*(?:Von|An|Cc)(?:\\s{,2}).*){2,})|^(?!Am.*Am\\s.+?schrieb.*:)(Am\\s(?:.+?\\s?)schrieb\\s(?:.+?\\s?.+?):)$|((?:(?:^|\\n)[* ]*(?:From|Sent|To|Subject|Date|Cc):[ *]*(?:\\s{,2}).*){2,}(?:\\n.*){,1})|^(?!On[.\\s]*On\\s(.+?\\s?.+?)\\swrote:)(On\\s(?:.+?\\s?.+?)\\swrote:)$|(?:(?:^|\\n)[* ]*(Von|Gesendet|An|Betreff|Datum):[ *]*(?:\\s{,2}).*){2,}|(^(> *))', flags=re.MULTILINE)
r.split(text)
However I'm getting back a lot of None and mix between matches and the reply body content. Not so sure why โ any idea? How I would imagine re.split to work:
[
'Latest reply.',
'Am So., 1. Jan. 2023 um 17:22 Uhr schrieb John Doe <\nnoreply@github.com>:\n\n\nSecond reply\n',
...
]Sample data and regex: https://regex101.com/r/cC1FUo/1
I suggest
l = re.compile("(?<!^)\s+(?=[A-Z])(?!.\s)").split(s)
Check this demo.
You could use a lookahead:
re.split(r' ', input)
This will split at every space that is followed by a string of upper-case letters which end in a word-boundary.
Note that the square brackets are only for readability and could as well be omitted.
If it is enough that the first letter of a word is upper case (so if you would want to split in front of Hello as well) it gets even easier:
re.split(r' ', input)
Now this splits at every space followed by any upper-case letter.
By using (,), you are capturing the group, if you simply remove them you will not have this problem.
>>> str1 = "a b c d"
>>> re.split(" +", str1)
['a', 'b', 'c', 'd']
However there is no need for regex, str.split without any delimiter specified will split this by whitespace for you. This would be the best way in this case.
>>> str1.split()
['a', 'b', 'c', 'd']
If you really wanted regex you can use this ('\s' represents whitespace and it's clearer):
>>> re.split("\s+", str1)
['a', 'b', 'c', 'd']
or you can find all non-whitespace characters
>>> re.findall(r'\S+',str1)
['a', 'b', 'c', 'd']
The str.split method will automatically remove all white space between items:
>>> str1 = "a b c d"
>>> str1.split()
['a', 'b', 'c', 'd']
Docs are here: http://docs.python.org/library/stdtypes.html#str.split