【发布时间】:2021-05-14 15:58:02
【问题描述】:
我的正则表达式模式似乎不适用于 Python。此列以逗号与电子表格分隔,在逗号之间还有分隔事物的 Pipes(|)。但是,我并不担心管道。我需要使用re.split() 方法用逗号分割字符串,但是,您会在示例中注意到用户在第一个| 之前的第一项中将逗号输入到字符串中——这就是我的原因用于使用 Regex 建立要查找的模式。但是,它不能正常工作,并且可以作为初学者使用另一组眼睛。我已经通过Regex101 构建并运行正则表达式来帮助我,并且解释似乎正确,但它仍然没有返回我期望的匹配数。
我的正则表达式模式:
".+\s\|\s\d\d\s\|\s\d\d\d\d\s\|\s\d\d\d\d\s\|\s.{2}\d\d\d\d\s\|\s\d+?\.\d+?,"gm
我的示例测试字符串:
ICS: Basic Maintenance | 30 | 5877 | 0000 | IT0000 | 12000.0,ICS: E-Rate discount (85%) | 30 | 5877 | 0000 | IT0000 | -10200.0,ICS: Basic Maintenance | 40 | 5877 | 0000 | IT0000 | 9000.0,ICMS: E-Rate discount (85%) | 40 | 5877 | 0000 | IT0000 | -7650.0,ICS: Basic Maintenance | 20 | 5877 | 0000 | IT0000 | 13500.0,ICS: E-Rate discount (85%) | 20 | 5877 | 0000 | IT0000 | -11475.0,ICCMS: Basic Maintenance | 70 | 5877 | 0000 | IT0000 | 12000.0,ICCMS: E-Rate discount (85%) | 70 | 5877 | 0000 | IT0000 | -10200.0,ITSM: Laptops, Desktops, Computers | 30 | 4400 | IT0000 | 720400.0
我期望的匹配数:9 匹配
我得到的匹配数:1 匹配 - (0-443):我从 Regex101 导出的匹配
"
.\s\|\s\d\d\s\|\s\d\d\d\d\s\|\s\d\d\d\d\s\|\s.\d\d\d\d\s\|\s\d\.\d,
"
gm
. matches any character (except for line terminators)
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\| matches the character | literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\| matches the character | literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\| matches the character | literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\| matches the character | literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
. matches any character (except for line terminators)
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\| matches the character | literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\d matches a digit (equivalent to [0-9])
+? matches the previous token between one and unlimited times, as few times as possible, expanding as needed (lazy)
\. matches the character . literally (case sensitive)
\d matches a digit (equivalent to [0-9])
, matches the character , literally (case sensitive)
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
0-443 ICS: Basic Maintenance | 30 | 5877 | 0000 | IT0000 | 12000.0,ICS: E-Rate discount (85%) | 30 | 5877 ...
Search reference
space
,
.+\s\|\s\d\d\s\|\s\d\d\d\d\s\|\s\d\d\d\d\s\|\s.{2}\d\d\d\d\s\|\s\d+?\.\d+?,
ICS: Basic Maintenance | 30 | 5877 | 0000 | IT0000 | 12000.0,ICS: E-Rate discount (85%) | 30 | 5877 | 0000 | IT0000 | -10200.0,ICS: Basic Maintenance | 40 | 5877 | 0000 | IT0000 | 9000.0,ICMS: E-Rate discount (85%) | 40 | 5877 | 0000 | IT0000 | -7650.0,ICS: Basic Maintenance | 20 | 5877 | 0000 | IT0000 | 13500.0,ICS: E-Rate discount (85%) | 20 | 5877 | 0000 | IT0000 | -11475.0,ICCMS: Basic Maintenance | 70 | 5877 | 0000 | IT0000 | 12000.0,ICCMS: E-Rate discount (85%) | 70 | 5877 | 0000 | IT0000 | -10200.0,ITSM: Laptops, Desktops, Computers | 30 | 4400 | IT0000 | 720400.0
ICS: Basic Maintenance | 30 | 5877 | 0000 | IT0000 | 12000.0,ICS: E-Rate discount (85%) | 30 | 5877 | 0000 | IT0000 | -10200.0,ICS: Basic Maintenance | 40 | 5877 | 0000 | IT0000 | 9000.0,ICMS: E-Rate discount (85%) | 40 | 5877 | 0000 | IT0000 | -7650.0,ICS: Basic Maintenance | 20 | 5877 | 0000 | IT0000 | 13500.0,ICS: E-Rate discount (85%) | 20 | 5877 | 0000 | IT0000 | -11475.0,ICCMS: Basic Maintenance | 70 | 5877 | 0000 | IT0000 | 12000.0,ICCMS: E-Rate discount (85%) | 70 | 5877 | 0000 | IT0000 | -10200.0,ITSM: Laptops, Desktops, Computers | 30 | 4400 | IT0000 | 720400.0
ICS: Basic Maintenance | 30 | 5877 | 0000 | IT0000 | 12000.0,ICS: E-Rate discount (85%) | 30 | 5877 | 0000 | IT0000 | -10200.0,ICS: Basic Maintenance | 40 | 5877 | 0000 | IT0000 | 9000.0,ICMS: E-Rate discount (85%) | 40 | 5877 | 0000 | IT0000 | -7650.0,ICS: Basic Maintenance | 20 | 5877 | 0000 | IT0000 | 13500.0,ICS: E-Rate discount (85%) | 20 | 5877 | 0000 | IT0000 | -11475.0,ICCMS: Basic Maintenance | 70 | 5877 | 0000 | IT0000 | 12000.0,```
【问题讨论】:
标签: python-3.x regex split