Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match last word in string ending in

Tags:

regex

I want to regex match the last word in a string where the string ends in ... The match should be the word preceding the ...

Example: "Do not match this. This sentence ends in the last word..."

The match would be word. This gets close: \b\s+([^.]*). However, I don't know how to make it work with only matching ... at the end.

This should NOT match: "Do not match this. This sentence ends in the last word."

like image 810
Paul P Avatar asked Oct 27 '25 08:10

Paul P


1 Answers

If you use \s+ it means there must be at least a single whitespace char preceding so in that case it will not match word... only.

If you want to use the negated character class, you could also use

([^\s.]+)\.{3}$
  • ( Capture group 1
    • [^\s.]+ Match 1+ times any char except a whitespace char or dot
  • ) Close group
  • \.{3} Match 3 dots
  • $ End of string

Regex demo

like image 127
The fourth bird Avatar answered Oct 30 '25 00:10

The fourth bird



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!