Is your feature request about something that is currently impossible or hard to do? Please describe the problem.
There is no simple way to find the last occurence of a pattern in a string. For simple strings strridx works, but it recommends to use match for patterns; however, match starts searching from the left. One can use the {start} and {count} arguments to select later matches, but that requires knowing in advance how many matches to skip.
Describe the solution you'd like
An argument for match, or a new function that begins searching for patterns starting from the end of a string. The simplest option is probably to allow a negative {count} for match, but if the way that interacts with {start} is too complex, a separate rmatch function would work too.
Describe alternatives you've considered
For some patterns reversing the string and subtracting the index from the length could work, but this is very prone to off-by-one (or more) errors and requires reversing the pattern as well, which could be pretty hard in general. A simpler option is to loop match and increase {count} by 1 until it stops matching.
Additional context
I know very little about regular expression matching so I don't know if what I'm requesting is possible to do more efficiently than the {count} workaround, although I'd be surprised if it wasn't. For context, the reason I need this is for a text formatting plugin that has to find the rightmost whitespace in a line. I'm currently just using strridx and only looking for ' ', but I'd also like to match tabs as well.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.![]()
Have you tried using a greedy quantifier to force the regex engine to start looking for a match from the end of the pattern?
:echo 'a b c d e'->match('.*\zs\s')
7
The greedy quantifier trick solves my case, thanks. split does too as long as the matches aren't overlapping. It still might be more convenient in general to allow a negative {count}, but if there's not much demand for it feel free to close this.
Closed #7875.
A reverse match() for (small and) large strings (Vim script):
https://gist.github.com/Houl/ca8d07dc07fc2823e1668851fe8a7d76
strridx() scans the string from left to right and then returns the last hit.
I guess a matchr() wouldn't do it any better.
For your use case: call strridx() separately for Space and Tab and then use the larger value.