Matching Anything But a Newline

  • + 8 comments

    The solution explained, piece by piece.

    The solution: ^...\....\....\....$

    This pattern expects strings like:

    111.222.333.444

    111.111.111.111

    aaa.aaa.aaa.aaa

    AAA.AAA.BBB.BBB

    abd.ghj.999.1n4

    That is, it must be 4 groups of 3 "anycharacters" delimited by periods. We use . to match "anycharacter" in regex. It matches everything but things like line breaks. And \. matches actual periods in the string. We have to escape the . because it is a metacharacter. So, in the solution, we're saying that between the beginning of the string ^ and the end of the string $ there will be exactly 4 groups of 3 "anycharacters" delimited by periods.