正则表达式不匹配空格

时间:2018-06-01 05:55:19

标签: python regex python-3.x

我不知道为什么这并不匹配所有的出现。我甚至把它放在网上的RE工具中,它应该匹配。我在Linux上运行3.6。

只有当搜索跟随' _SINK'但不是在' \ s'。

之后

以下是我的搜索文本片段和RE:

import re

lines = ['R_d_10763 VDDI_1007 Group_BGA_BGA_VDD_INT_AD26 3.711438e+000',
         'R_d_10771 VDDI_1007 VDDI_1012 1.337785e+001',
         'R_d_8607 Group_BGA_BGA_VSS_AH6 Group_BGA_BGA_VSS_AJ43 3.777161e+000',
         'R_d_6585 Group_BGA_BGA_AVDD085_MIPI_DPHY_Y36 Group_BGA_BGA_AVDD085_MIPI_DPHY_W35_SINK_ 3.860682e-003',
         'R_d_69804 Xm4s4s2_M_DPDATA0_580 Group_BGA_BGA_XMIPI_DSI_D0_P_V38_SINK_ 2.668494e-001',
         'R_d_69668 XCP_ANT_SW1_383 Group_BGA_BGA_XCP_ANT_SW1_N5_SINK_ 5.037550e-001',
         'R_d_13 AVDD085_MIPI_DPHY_540 Group_BGA_BGA_AVDD085_MIPI_DPHY_Y36 2.854267e-002',]


for line in lines:
    conn1, conn2, res = line.split()[1:]
    match1 = re.search(r'(A?[A-Z][0-9]{1,2})(?:_SINK|\s)', conn1)
    match2 = re.search(r'(A?[A-Z][0-9]{1,2})(?:_SINK|\s)', conn2)
    try:
        conn1 = match1.group(1)
    except AttributeError:
        pass
    try:
        conn2 = match2.group(1)
    except AttributeError:
        pass
    print(f'Connection 1: {conn1}  Connection 2: {conn2}  Resistance: {res}')

我得到的输出是:

Connection 1: VDDI_1007  Connection 2: Group_BGA_BGA_VDD_INT_AD26  Resistance: 3.711438
Connection 1: VDDI_1007  Connection 2: VDDI_1012  Resistance: 13.37785
Connection 1: Group_BGA_BGA_VSS_AH6  Connection 2: Group_BGA_BGA_VSS_AJ43  Resistance: 3.777161
Connection 1: Group_BGA_BGA_AVDD085_MIPI_DPHY_Y36  Connection 2: W35  Resistance: 0.003860682
Connection 1: Xm4s4s2_M_DPDATA0_580  Connection 2: V38  Resistance: 0.2668494
Connection 1: XCP_ANT_SW1_383  Connection 2: N5  Resistance: 0.5037550
Connection 1: AVDD085_MIPI_DPHY_540  Connection 2: Group_BGA_BGA_AVDD085_MIPI_DPHY_Y36  Resistance: 0.02854267

但它应该是

Connection 1: VDDI_1007  Connection 2: AD26  Resistance: 3.711438
Connection 1: VDDI_1007  Connection 2: VDDI_1012  Resistance: 13.37785
Connection 1: AH6  Connection 2: AJ43  Resistance: 3.777161
Connection 1: Y36  Connection 2: W35  Resistance: 0.003860682
Connection 1: Xm4s4s2_M_DPDATA0_580  Connection 2: V38  Resistance: 0.2668494
Connection 1: XCP_ANT_SW1_383  Connection 2: N5  Resistance: 0.5037550
Connection 1: AVDD085_MIPI_DPHY_540  Connection 2: Y36  Resistance: 0.02854267

谢谢,

2 个答案:

答案 0 :(得分:0)

问题在于,当您拆分字符串时,会删除空格,因此当您在正则表达式中查找尾随空格时,搜索将失败。

在你的情况下,

line = "R_d_10763 VDDI_1007 Group_BGA_BGA_VDD_INT_AD26 3.711438e+000"
conn1, conn2, res = line.split()[1:]

print(conn2) # notice that trailing whitespace is gone
# 'Group_BGA_BGA_VDD_INT_AD26'

re.search(r'(A?[A-Z][0-9]{1,2})(?:_SINK|\s)', conn2)
# no match

这是一个有效的版本..

import re

lines = ['R_d_10763     VDDI_1007                             Group_BGA_BGA_VDD_INT_AD26                  3.711438e+000',
         'R_d_10771     VDDI_1007                             VDDI_1012                                   1.337785e+001',
         'R_d_8607      Group_BGA_BGA_VSS_AH6                 Group_BGA_BGA_VSS_AJ43                      3.777161e+000',
         'R_d_6585      Group_BGA_BGA_AVDD085_MIPI_DPHY_Y36   Group_BGA_BGA_AVDD085_MIPI_DPHY_W35_SINK_   3.860682e-003',
         'R_d_69804     Xm4s4s2_M_DPDATA0_580                 Group_BGA_BGA_XMIPI_DSI_D0_P_V38_SINK_      2.668494e-001',
         'R_d_69668     XCP_ANT_SW1_383                       Group_BGA_BGA_XCP_ANT_SW1_N5_SINK_ 5.037550e-001',
         'R_d_13        AVDD085_MIPI_DPHY_540                 Group_BGA_BGA_AVDD085_MIPI_DPHY_Y36 2.854267e-002',]


for line in lines:
    conn1, conn2, res = line.split()[1:]
    match1 = re.search(r'(?<=_)[A-Z]{1,2}\d{1,2}(?=_SINK|$)', conn1)
    match2 = re.search(r'(?<=_)[A-Z]{1,2}\d{1,2}(?=_SINK|$)', conn2)
    try:
        conn1 = match1.group()
    except AttributeError:
        pass
    try:
        conn2 = match2.group()
    except AttributeError:
        pass
    print(f'{conn1:40} -- {conn2:40} -- {res:40}')

输出:

Connection 1: VDDI_1007  Connection 2: AD26  Resistance: 3.711438e+000
Connection 1: VDDI_1007  Connection 2: VDDI_1012  Resistance: 1.337785e+001
Connection 1: AH6  Connection 2: AJ43  Resistance: 3.777161e+000
Connection 1: Y36  Connection 2: W35  Resistance: 3.860682e-003
Connection 1: Xm4s4s2_M_DPDATA0_580  Connection 2: V38  Resistance: 2.668494e-001
Connection 1: XCP_ANT_SW1_383  Connection 2: N5  Resistance: 5.037550e-001
Connection 1: AVDD085_MIPI_DPHY_540  Connection 2: Y36  Resistance: 2.854267e-002

答案 1 :(得分:0)

只需将\s替换为\b

import re

lines = [
    "R_d_10763 VDDI_1007 Group_BGA_BGA_VDD_INT_AD26 3.711438e+000",
    "R_d_10771 VDDI_1007 VDDI_1012 1.337785e+001",
    "R_d_8607 Group_BGA_BGA_VSS_AH6 Group_BGA_BGA_VSS_AJ43 3.777161e+000",
    "R_d_6585 Group_BGA_BGA_AVDD085_MIPI_DPHY_Y36 Group_BGA_BGA_AVDD085_MIPI_DPHY_W35_SINK_ 3.860682e-003",
    "R_d_69804 Xm4s4s2_M_DPDATA0_580 Group_BGA_BGA_XMIPI_DSI_D0_P_V38_SINK_ 2.668494e-001",
    "R_d_69668 XCP_ANT_SW1_383 Group_BGA_BGA_XCP_ANT_SW1_N5_SINK_ 5.037550e-001",
    "R_d_13 AVDD085_MIPI_DPHY_540 Group_BGA_BGA_AVDD085_MIPI_DPHY_Y36 2.854267e-002",
]


for line in lines:
    conn1, conn2, res = line.split()[1:]
    match1 = re.search(r"(A?[A-Z][0-9]{1,2})(?:_SINK|\b)", conn1)
    match2 = re.search(r"(A?[A-Z][0-9]{1,2})(?:_SINK|\b)", conn2)
    try:
        conn1 = match1.group(1)
    except AttributeError:
        pass
    try:
        conn2 = match2.group(1)
    except AttributeError:
        pass
    print(f"Connection 1: {conn1}  Connection 2: {conn2}  Resistance: {res}")

输出:

Connection 1: VDDI_1007  Connection 2: AD26  Resistance: 3.711438e+000
Connection 1: VDDI_1007  Connection 2: VDDI_1012  Resistance: 1.337785e+001
Connection 1: AH6  Connection 2: AJ43  Resistance: 3.777161e+000
Connection 1: Y36  Connection 2: W35  Resistance: 3.860682e-003
Connection 1: Xm4s4s2_M_DPDATA0_580  Connection 2: V38  Resistance: 2.668494e-001
Connection 1: XCP_ANT_SW1_383  Connection 2: N5  Resistance: 5.037550e-001
Connection 1: AVDD085_MIPI_DPHY_540  Connection 2: Y36  Resistance: 2.854267e-002