Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

String.gsub() to extract last word in string

I have a string where the words within are separated by periods, what would be the best way for returning the last word?
My guess would be to use string.gsub, but i’m having a hard time figuring out the correct syntax

here’s an example:

str = "These.Are.A.Few.Words.But.I_only_want_this"

Desired output would be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I_only_want_this

Note that i don’t need to treat any magical characters as such, they can stay in the string no problems

>Solution :

Try:

str:gsub('.*%.', '')

.* – Match zero or more (*) of any character (.), taking the longest sequence possible.
%. – Match a literal . character.

Example:

Lua 5.4.6  Copyright (C) 1994-2023 Lua.org, PUC-Rio
> str = "These.Are.A.Few.Words.But.I_only_want_this"
> str:gsub('.*%.', '')
I_only_want_this    1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading