Back References
Some versions of ksh allow for back references. Back references allow a sub-pattern to be referred to by number. The number refers to the instance of the open parentheses (counting from the left) starting with 1. Within a pattern, back references are referred to by a backslash followed by the number just mentioned. Therefore, the following example will match any string that begins and ends with the same letter, such as “mom”, “dad”, or “dumb-founded”:
@(?)*\1
Here is the breakdown of the expression. The @ means to match exactly one pattern; the (?) says to match exactly one character. The * says to match zero or more characters, and the \1 says to go back and reference what was after the first parenthesis. Therefore, it matches ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access