I have the following regex \b(\w+)$
that works to find the last word in a string. However the longer the string, the more steps it takes.
How can I make it start the search from the end of the line?
I have the following regex \b(\w+)$
that works to find the last word in a string. However the longer the string, the more steps it takes.
How can I make it start the search from the end of the line?
Using the regex you specified \b(\w+)$
you will get an increasing number of steps depending on the string's length (it will match each \b
, then each \b\w
, then each \b\w\w
until it finds a proper match of \b\w$
), but it still has to do that check on each item in the string until it's satisfied.
What you can do to get the last item of a string using regex explicitly is to flip the string and then use the ^
anchor. This will cause regex to immediately be satisfied upon the first match and stop processing.
You can search how to flip a string in multiple languages. Some examples for languages include the following:
You can see the regex in use here
Your programming language
// flip string code goes here
Regex
^(\w+)
Your programming language
// flip regex capture code goes here
This is my string
Converted to the following by flipping the string in your language
gnirts ym si sihT
Regex returns the following result
gnirts
Flip the string back in your language
string
Since the anchor ^
is used, it will check from the beginning of the string (as per usual regex behaviour). If this is satisfied it will return the match, otherwise, it will return no matches. Testing in regex101 (provided through the link in the Code section) shows that it takes exactly 6 steps to ensure that a match is made. It also takes exactly 3 steps to ensure no match is made. These values do not change with string length.
In most regex engines, you can't.
Regex engines work by consuming input from the start of the input.
You can programmatically do it with a simple decrementing loop over the characters starting from the last character. If you need more performance, using code over regex is the only way.
It only works in .NET
:
Regex rx = new Regex(Pattern, RegexOptions.RightToLeft);
Match match = rx.Match(Source);
This can be faster.
^.*\b(\w+)
• add ^.*
before and capture \w+
• drop the $
if possible
Good luck!