/r (Right-to-Left)
What is /r (Right-to-Left)?
/r (Right-to-Left): Scans the input from right to left instead of left to right — matters for lookaround direction and where scanning starts. Right-to-left mode, available in .NET as RegexOptions.RightToLeft, makes the engine start at the end of the text and move backwards. The first match it reports is the last one in the string.
Frequently asked questions about /r (Right-to-Left)
How is /r (Right-to-Left) used in practice?
That is handy when you want the final occurrence of something, such as the file extension after the last dot, or the last number in a line. With normal matching you would need a greedy prefix or extra code to find it.
What should you keep in mind with /r (Right-to-Left)?
It also changes how lookarounds and some quantifiers behave, so a pattern that works left to right may give different results in this mode. Most other engines do not support it at all. If your code may move to another language, prefer a portable approach, such as matching with $ at the end, or splitting the string and taking the last part.
At a glance
| Category | Regex flags |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |