gpt4 book ai didi

python - 在 Python 3.7 中拆分字符串的正则表达式

转载 作者:太空宇宙 更新时间:2023-11-04 04:09:37 25 4
gpt4 key购买 nike

我正在尝试拆分我在 python 程序中从 excel 文件中读取的一系列非常规字符串。我正在使用 Regex101.com 进行测试,我已经部分成功了:
我的样本:

Barber #1-1 Daily Prod. - Pumping unit  
Barbee #1-3 Daily Prod. - Plunger Lift
Barbee #1-5 Daily Prod. = Coil Tubing
Barbee #1-3 Daily Prod. - Plunger
Barbee #1-5 Daily Prod.w/ coil tubing
Porter GU #1 Well #2 Daily Prod.
Barber GU #1 Well #1 Daily Prod.
Bogel #1-2 Daily Prod. w/ plunger

我的正则表达式:
(.*)\sDaily Prod\.(.*$)

我得到这个答案选择 group1 和 group2:

Barber #1-1 - Pumping unit  
Barbee #1-3 - Plunger Lift
Barbee #1-5 = Coil Tubing
Barbee #1-3 - Plunger
Barbee #1-5w/ coil tubing
Porter GU #1 Well #2
Barber GU #1 Well #1
Bogel #1-2 w/ plunger

我想要:

Barber #1-1 Pumping unit  
Barbee #1-3 Plunger Lift
Barbee #1-5 Coil Tubing
Barbee #1-3 Plunger
Barbee #1-5 coil tubing
Porter GU #1 Well #2
Barber GU #1 Well #1
Bogel #1-2 plunger

谢谢。

最佳答案

我的猜测是这个表达式可能会起作用:

(.*)\sDaily Prod\.(\s*[-=w\/]+\s*)?(.*)

这里,我们有一个可选组:

(\s*[-=w\/]+\s*)?

我们收集不需要的字符和空格,然后用 $1 和 $3 进行替换。

Demo

测试

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"(.*)\sDaily Prod\.(\s*[-=w\/]+\s*)?(.*)"

test_str = ("Barber #1-1 Daily Prod. - Pumping unit\n"
"Barbee #1-3 Daily Prod. - Plunger Lift\n"
"Barbee #1-5 Daily Prod. = Coil Tubing\n"
"Barbee #1-3 Daily Prod. - Plunger\n"
"Barbee #1-5 Daily Prod.w/ coil tubing\n"
"Porter GU #1 Well #2 Daily Prod.\n"
"Barber GU #1 Well #1 Daily Prod.\n"
"Bogel #1-2 Daily Prod. w/ plunger")

subst = "\\1 \\3"

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)

if result:
print (result)

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

正则表达式电路

jex.im可视化正则表达式:

enter image description here

关于python - 在 Python 3.7 中拆分字符串的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56571543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com