如何使用python分割input_data?

时间:2019-02-19 05:30:56

标签: python regex split

更新的问题

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeWindowContentChanged|typeViewFocused"
android:accessibilityFlags="flagIncludeNotImportantViews|flagReportViewIds"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true"
android:packageNames="com.whatsapp"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="500"
/>

使用tag_list拆分上述输入数据

input_data = 'ABCD:Data ScientistPBCD:Python Developer QBCD:Artificial IntelligenceBCD:Machine Learning Developer'

tag_list = ["ABCD", "BCD", "PBCD", "QBCD"]

执行上面的代码时,我得到下面的错误输出

for each_tag in tag_list:
    input_data = input_data.replace(each_tag, "|" + each_tag)

print(input_data)

我期望以下输出:-

Error Output:-
|A|BCD:Data Scientist|P|BCD:Python Developer |Q|BCD:Artificial Intelligence|BCD:Machine Learning Developer

如何解决以上错误?

请检查以下代码:- 我已经测试了所有新代码,当图片中出现新数据时出现错误

Expected Output :-
|ABCD:Data Scientist|PBCD:Python Developer |QBCD:Artificial Intelligence|BCD:Machine Learning Developer

请检查下图,我用黄色突出显示了未被分割的部分

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用替换方法

此正则表达式将匹配所需的内容

([A-Z]+?(?=:))

并替换为!\1

  • ([A-Z]+?(?=:))-将A到Z匹配一次或多次,然后是:

Demo

答案 1 :(得分:0)

如果您的代码详尽无遗,则可以执行以下操作:

import re

input_data = 'ABCD:Data ScientistPBCD:Python Developer QBCD:Artificial IntelligenceBCD:Machine Learning Developer'

tag_list = ["ABCD", "BCD", "PBCD", "QBCD"]
pattern = r'([A|P|Q]{0,1}BCD)'
subbed_data = re.sub(pattern, r'|\1', input_data)

>>>subbed_data
'|ABCD:Data Scientist|PBCD:Python Developer |QBCD:Artificial Intelligence|BCD:Machine Learning Developer'