[Python] How does the Python "re" module find line endings?

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to use the Python "re" module with methods "search" and "sub", but without success.

    I read a text file having DOS/Windows line endings into a string variable:

    import re
    with open("filename", 'r') as input:
    fileContent = input.read()


    Variable "fileContent" looks like this:

    "Some text.\r\n\r\nSome more text."


    I want to replace some of the line endings with a pipe ("|") symbol:

    result = re.sub(r'\n\r\n', '|', fileContent)


    A substitution never occurs. It never finds the 0x0a0d0a byte pattern. The same is true using the search method. Same when using the MULTILINE flag. What am I doing wrong?

    I tried using "re.search()" to find the pattern without success. I tried expressing the regex pattern as raw text and as a normal string. The method never finds the 3-byte sequence.

    Continue reading...

Compartilhe esta Página