使用以下代码(有点麻烦,我承认)我用逗号分隔一个字符串,但条件是当字符串包含逗号分隔的单个单词时它不会分开,例如:
它不会将"Yup, there's a reason why you want to hit the sack just minutes after climax"
分开,但会将"The increase in heart rate, which you get from masturbating, is directly beneficial to the circulation, and can reduce the likelihood of a heart attack"
分隔为['The increase in heart rate', 'which you get from masturbating', 'is directly beneficial to the circulation', 'and can reduce the likelihood of a heart attack']
问题是代码在遇到这样的字符串时失败的目的:"When men ejaculate, it releases a slew of chemicals including oxytocin, vasopressin, and prolactin, all of which naturally help you hit the pillow."
我不想在催产素后分离,而是在催乳素后分离。我需要一个正则表达式来做到这一点。
import os
import textwrap
import re
import io
from textblob import TextBlob
string = str(input_string)
listy= [x.strip() for x in string.split(',')]
listy = [x.replace('\n', '') for x in listy]
listy = [re.sub('(?<!\d)\.(?!\d)', '', x) for x in listy]
listy = filter(None, listy) # Remove any empty strings
newstring= []
for segment in listy:
wc = TextBlob(segment).word_counts
if listy[len(listy)-1] != segment:
if len(wc) > 3: # len(segment.split(' ')) > 7:
newstring.append(segment+"&&")
else:
newstring.append(segment+",")
else:
newstring.append(segment)
sep = [x.strip() for x in (' '.join(newstring)).split('&&')]
答案 0 :(得分:1)
考虑以下..
mystr="When men ejaculate, it releases a slew of chemicals including oxytocin, vasopressin, and prolactin, all of which naturally help you hit the pillow."
rExp=r",(?!\s+(?:and\s+)?\w+,)"
mylst=re.compile(rExp).split(mystr)
print(mylst)
应该给出以下输出..
['When men ejaculate', ' it releases a slew of chemicals including oxytocin, vasopressin, and prolactin', ' all of which naturally help you hit the pillow.']
让我们看一下我们如何分割字符串......
,(?!\s+\w+,)
使用未跟随的所有逗号((?!
- &gt;负向前看)\s+\w+,
空格和带逗号的单词。
如果vasopressin, and
and
,
后面没有and\s+
,则上述情况会失败。因此,在。内引入条件,(?!\s+(?:and\s+)?\w+,)
。
,(?!\s+(?:(?:and|or)\s+)?\w+,)
虽然我可能想使用下面的
listy= [x.strip() for x in string.split(',')]
实质上考虑更换你的线
listy= [x.strip() for x in re.split(r",(?!\s+(?:and\s+)?\w+,)",string)]
与
clf;
drawlater()
param3d(x,y,z);
set(gce(),"mark_mode","on","mark_style",2,"mark_size_unit","point","mark_size",4,"line_mode","off")
drawnow()