[Python] How to select closest occurrence of the expected output with Regex python? [closed]

Discussão em 'Python' iniciado por Stack, Fevereiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I have large text document with very noisy text details as follows: here d5 is not a typo. It is noisy text document.

    Content = """
    [2025-02-21 15:53:05.161] }/d_5
    [2025-02-21 15:53:05.161]
    [2025-02-21 15:53:06.123] d5{
    [2025-02-21 15:53:06.155] 0.160,0.1130,
    [2025-02-21 15:53:06.155] 3.4283.430,3.420,
    [2025-02-21 15:53:06.155] 3.434,3437,3.444,3.444,3.4413.412,3.432,3.434,3.429,
    [2025-02-21 15:53:06.155] 34.664,26.739,26.977,26.885,26.968,34.703,
    [2025-02-21 15:53:06.155] 102.997,101.816,
    [2025-02-21 15:53:06.155] 6.000,100%,
    [2025-02-21 15:53:06.155] 0x0000,0x0043,
    [2025-02-21 15:53:06.155] 43.450,40.010,[0x0000][0x0000],0,246328
    [2025-02-21 15:53:06.155] }/d_5
    [2025-02-21 15:53:06.155]
    [2025-02-21 15:53:07.165] d_5{
    [2025-02-21 15:53:07.165] 0.150,0.1130,
    [2025-02-21 15:53:07.165] 3.428,3.437,3.432,3.431,3.429,3.422,3.434,�.430,
    [2025-02-21 15:53:07.165] 3.434,3.437,3.43",3.434,3.429,
    [2025-02-21 15:53:07.165] 34.669,26.739,26.981,26.885,26.968,34.715,
    [2025-02-21 15:53:07.165] 102.993,101.813,
    [2025-02-21 15:53:07.165] 6.000,100%,
    [2025-02-21 15:53:07.165] 0x0000(0x0043,
    [2025-02-21 15:53:07.165] 43.430,40.030,[0x0000][0x0000],0,247331
    [2025-02-21 15:53:07.165] }/d_5
    [2025-02-21 15:53:07.165]
    """


    Not getting expected results with below trial.

    content = re.findall(r'[d_5\{].*?\}',content,flags=re.I|re.DOTALL|re.M)


    Expecting Output Like through the text document:

    [2025-02-21 15:53:06.123] d5{
    [2025-02-21 15:53:06.155] 0.160,0.1130,
    [2025-02-21 15:53:06.155] 3.4283.430,3.420,
    [2025-02-21 15:53:06.155] 3.434,3437,3.444,3.444,3.4413.412,3.432,3.434,3.429,
    [2025-02-21 15:53:06.155] 34.664,26.739,26.977,26.885,26.968,34.703,
    [2025-02-21 15:53:06.155] 102.997,101.816,
    [2025-02-21 15:53:06.155] 6.000,100%,
    [2025-02-21 15:53:06.155] 0x0000,0x0043,
    [2025-02-21 15:53:06.155] 43.450,40.010,[0x0000][0x0000],0,246328
    [2025-02-21 15:53:06.155] }/d_5

    [2025-02-21 15:53:07.165] d_5{
    [2025-02-21 15:53:07.165] 0.150,0.1130,
    [2025-02-21 15:53:07.165] 3.428,3.437,3.432,3.431,3.429,3.422,3.434,�.430,
    [2025-02-21 15:53:07.165] 3.434,3.437,3.43",3.434,3.429,
    [2025-02-21 15:53:07.165] 34.669,26.739,26.981,26.885,26.968,34.715,
    [2025-02-21 15:53:07.165] 102.993,101.813,
    [2025-02-21 15:53:07.165] 6.000,100%,
    [2025-02-21 15:53:07.165] 0x0000(0x0043,
    [2025-02-21 15:53:07.165] 43.430,40.030,[0x0000][0x0000],0,247331
    [2025-02-21 15:53:07.165] }/d_5

    Continue reading...

Compartilhe esta Página