Regex end of line ($) Didn't Work

Just trying to learn about regex, and frustratingly spent some time to figure out why the end-of-line sign did not work. Turns out, the text file format is the culprit.

Text file refers to the file format of plain text. Each operating system has its own type and it affects the line endings:

  • Windows (DOS): carriage return and line feed, or CR-LF. \r\n
  • Nix: LF. \n

A line in DOS text file contains an extra carriage return character at the end of line, the \r. Which makes $ regex won't be able to detect it. Or another way of saying it is that end-of-line regex sign only applies to \n characters.

So there are 2 ways to fix it:

  • Convert the File
    Simply use dos2unix To convert a CR-LF to LF.

  • Use the Proper Regex
    The correct way to detect DOS file format is with \r$. A literal carriage return character and the $ sign. This will match DOS format.
    Another way is to use [[:space:]]$. In which "the [[:space:]] bit will match any single "space-like character", and this includes the carriage-return."[1]

  1. ↩︎