简体   繁体   中英

Python find position in a string

I have an input string mentioned below.

`input="N.ACM_CONTRACT_TR_STATUS,     N.ACM_REVIEW_DDQ,     N.ACM_IS_GO_IND,   AS  N.ACM_CHARITY_REGISTERED_IND"`

The output should be as below : 'AS' keyword should be removed. How can I find the position of the comma occurring just before ' AS '. Is there a method in python other than find() which works similar as INSTR in Oracle.

"N.ACM_CONTRACT_TR_STATUS,     N.ACM_REVIEW_DDQ,     N.ACM_IS_GO_IND,    N.ACM_CHARITY_REGISTERED_IND"`
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)

The comment on this post is correct; I apologize, I am very tired.

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)

The new string is 'N.ACM_CONTRACT_TR_STATUS, N.ACM_REVIEW_DDQ, N.ACM_IS_GO_IND N.ACM_CHARITY_REGISTERED_IND' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM