I have several strings that look like
"Abcde fgh 123,456"
I want to split it as
["Abcde fgh", "123,456"]
I tried
string = "Abcde fgh 123,456"
re.split(r'(\d+)', string)
But this gives
["Abcde fgh", "123", "," "456"]
You get that result because you use a capturing group with split which will also return the captured text.
You could use a positive lookahead instead. (?=\\d)
would work, but to be more precise for your example data you could also use:
(?<!\\d)\\s+(?=\\d+,\\d+)
import re
string = "Abcde fgh 123,456"
print(re.split(r'(?<!\d)\s+(?=\d+,\d+)', string))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.