Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
I have a couple of pets, e.g., cat, rat, and fish. What about you?
我想按句点分割上面的文本。如果我使用正则表达式\.来拆分它,我会得到["I have a couple of pets, e","g",", cat, rat, and fish"," What about you?"],这不是我想要的。
\.
["I have a couple of pets, e","g",", cat, rat, and fish"," What about you?"]
如何添加限制,\.以便字母前面的句点e不会g被视为拆分器?
e
g
使用否定的向后看:
(?<!e|g)\.
我不确定以上内容是否适用于 Python。如果没有,试试这个:
(?<!e)(?<!g)\.
如果您尝试拆分句子,则应使用以下正则表达式:
\.(?=\s)
这将检测后跟空白字符的句点。
也许您还想检测...或分号?
...
那么你应该试试这个:
(\.|\.{3}|;)(?=\s)
您还可以考虑结束一行的句点:
(\.|\.{3}|;)(?=(\s|$))
尝试消极的回顾。
供你参考:
正则表达式环顾四周