text = 'Transaction value was, - 3456.63 Rupees'
regex = r"(?<!\d)[" + string.punctuation + "](?!\d)"
result = re.sub(regex, "", text)
# output: 'Transaction value was 3456.63 Rupees'
要解决第二个问题,请尝试使用以下技巧:
text = 'Transaction value was, - Rs.3456.63'
regex_space = r"([0-9]+(\.[0-9]+)?)"
regex_punct = r'[^\w.]+'
re.sub(r'[^\w.]+', ' ', re.sub(regex_space,r" \1 ", text).strip())
# output: 'Transaction value was Rs. 3456.63 Rupees'
如果我正确理解您的问题,则此代码适用于您:
要解决第二个问题,请尝试使用以下技巧:
您可以使用更通用的正则表达式来替换除以下字符以外的所有特殊字符。你知道吗
你得到了吗
EDIT:下面的函数只返回带小数的数字。你知道吗
相关问题 更多 >
编程相关推荐