Python在字符串中查找位置

时间:2017-05-06 18:25:02

标签: python python-3.x

我有一个下面提到的输入字符串。

"N.ACM_CONTRACT_TR_STATUS,     N.ACM_REVIEW_DDQ,     N.ACM_IS_GO_IND,    N.ACM_CHARITY_REGISTERED_IND"`

输出应如下所示:' AS'应删除关键字。如何找到逗号之前发生的逗号的位置? AS'。除了find()之外,python中是否有一个方法与Oracle中的INSTR类似。

# models.py

steps = ['passo_open','passo_edit','passo_add_file','passo_judge']

step = process.get_current_step()

print step 
#prints 'passo_edit'

2 个答案:

答案 0 :(得分:0)

import re

s = "N.ACM_CONTRACT_TR_STATUS,     N.ACM_REVIEW_DDQ,     N.ACM_IS_GO_IND,   AS  N.ACM_CHARITY_REGISTERED_IN"
re.sub(r",(\s+)AS(\s+)", r"\1,\2", s)

答案 1 :(得分:-1)

对这篇文章的评论是正确的;我道歉,我很累。

import re

text="N.ACM_CONTRACT_TR_STATUS,     N.ACM_REVIEW_DDQ,     N.ACM_IS_GO_IND,   AS  N.ACM_CHARITY_REGISTERED_IND"
# Matches comma, any whitespace between comma and 'AS', and 'AS' itself,
# but only if 'AS' is a word in its own right.
unwanted_chars_re = re.compile(r",\s*AS\b")
new_text = unwanted_chars_re.sub('', text)

新字符串为'N.ACM_CONTRACT_TR_STATUS, N.ACM_REVIEW_DDQ, N.ACM_IS_GO_IND N.ACM_CHARITY_REGISTERED_IND'