Regex to find last occurrence of pattern in a string

Seamus picture Seamus · Jan 26, 2017 · Viewed 19.1k times · Source

My string being of the form:

"as.asd.sd fdsfs. dfsd  d.sdfsd. sdfsdf sd   .COM"

I only want to match against the last segment of whitespace before the last period(.)

So far I am able to capture whitespace but not the very last occurrence using:

\s+(?=\.\w)

How can I make it less greedy?

Answer

Mohammad Yusuf picture Mohammad Yusuf · Jan 26, 2017

You can try like so:

(\s+)(?=\.[^.]+$)

(?=\.[^.]+$) Positive look ahead for a dot and characters except dot at the end of line.

Demo:

https://regex101.com/r/k9VwC6/3