Python regex "not"

In Python, I want to be comma-separated, but not if the trailing space follows it

To break into a comma, I

(?:[^,]+)

Im looking for something like

(?:[^,]+)(?!:[^, ]+)

An example of a situation:

"Me, Myself & Irene,The Cable Guy" 

should get:

"Me, Myself & Irene"
"The Cable Guy"
+4
source share
2 answers

You want to get a negative acknowledgment forward (?!...). This matches any position that does not have a specified template. To separate commas that are not followed by a space, these are:

,(?! )

You re.split()do not need any other grouping.

Demo:

>>> import re
>>> re.split(r',(?! )', "Me, Myself & Irene,The Cable Guy")
['Me, Myself & Irene', 'The Cable Guy']
+5
source

Let me explain what is wrong with the original approach.

1+ [^,]+ , (?!:[^, ]+), , . , (?:[^,]+)(?!:[^, ]+) 1 + , :, 1 , , .

, , ,, 1 , , 0+ :

re.findall(r'[^,]+(?:,\s+[^,]+)*', s)

regex

. - Python:

import re
rx = r"[^,]+(?:,\s[^,]+)*"
s = "Me, Myself & Irene,The Cable Guy"
print(re.findall(rx,s))
# => ['Me, Myself & Irene', 'The Cable Guy']

, ,(?!\s) .

0

Source: https://habr.com/ru/post/1676919/


All Articles