Regular Expressions

Anchor Characters: Dollar ($)

Remarks#

A great deal of regex engines use a “multi-line” mode in order to search several lines in a file independently.

Therefore when using $, these engines will match all lines’ endings. However, engines that do not use this kind of multi-line mode will only match the last position of the string provided for the search.

Match a letter at the end of a line or string

g$

The above matches one letter (the letter g) at the end of a string in most regex engines (not in Oniguruma, where the $ anchor matches the end of a line by default, and the m (MULTILINE) modifier is used to make a . match any characters including line break characters, as a DOTALL modifier in most other NFA regex flavors). The $ anchor will match the first occurrence of a g letter before the end of the following strings:

In the following sentences, only the letters in bold match:

Anchors are characters that, in fact, do not match any character in a string

Their goal is to match a specific position in that string.

Bob was helping

But his edit introduced examples that were not matching!

In most regular expression flavors, the $ anchor can also match before a newline character or line break character (sequence), in a MULTILINE mode, where $ matches at the end of every line instead of only at the end of a string. For example, using g$ as our regex again, in multiline mode, the italicised characters in the following string would match:

tvxlt obofh necpu riist g\n aelxk zlhdx lyogu vcbke pzyay wtsea wbrju jztg\n drosf ywhed bykie lqmzg wgyhc lg\n qewrx ozrvm jwenx

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow