Search code examples
regexnestedeol

What is the behaviour of $ in regex expressions?


What is the behaviour of $ when used in between or multiple times in a regex expression? According to the Regex Wiki "$" should match end of line. Can I use $ multiple times in a regex to match consequent lines?

Consider the following regex:

^(a$|b)(c)$

Why does the above regex not match the string "a\nc\n". You can check the same at https://regexr.com/4b84o.

The regex seems to stop validating once "a\n" is reached and "c\n" is not validated consequently but is taken for the next string to be matched. Do regex expressions not validate for multiple lines? Does $ denote end of line (i.e \n character) or does it match for the end of string that is to be matched?


Solution

  • First, you don't have the m flag enabled, so $ matches the end of string.

    Even if you had the m flag enabled, the regex still would not match.

    This is because $ only matches the position of the end of the line i.e. it is a 0-length match, not the new line character \n. To match a new line character, you use \n.

    So what (a$|b)(c)$ is saying is:

    There will be an a and that will be the end of the line, or b. After that, there is a c. And c is the end of a line as well.

    That is self-contradicting because if a is at the end of the line, there will be a new line character (or nothing at all) after it, so there can't be c. Therefore, your regex won't match anything.

    Use the \n character instead:

    (a\n|b)c\n