我正在尝试拆分我在 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可视化正则表达式:
