我正在使用一个简单的正则表达式来尝试将字符串分类为与权利(工资)或其他相关的字符串。
我已经在regex101.com中测试了正则表达式,它似乎按预期工作但是当我运行代码时,我得到一些奇怪的结果
import re
wages_regx = re.compile(r" pay | payment | salary | salaries | wages? | rates? | hourly rates? | allowances? | penalties | penalty | overtime | under payments? | over payments? | entitlements? ", re.I)
def classify(string):
if wages_regx.match(string):
return 'Entitlements'
else:
return 'Other'
测试
classify('I have an question about my wages rate.')
# Returns: Other (Should return Entitlements) should match on (wages? and rates?)
classify(' wages ')
# Returns Entitlements as expected
classify('ras wages c')
# Returns Other (Should return Entitlements)
classify(' wages c')
# Returns Entitlements as expected