Main question
Answers to the question Automatically colour previously defined strings of text show how to colour pre-defined strings of text, but all those pre-defined strings are without spaces and punctuation marks.
I would like to ask if it were possible to design a MWE with (more or less, but without specificities such as; I quote; "only the endings of words") analogue function, but which additionally is functional also for strings with spaces and/or punctuation marks.
I am sorry if this is a duplicate. If so, I would be happy to be shown the way (to the duplicate).
Suggested MWE strings, colouring rules and trial text
Since it was suggested (in the comments) for me to propose some strings, colouring rules, and a piece of text to test the MWE, I have created the following poetic piece:
\documentclass{article}
\begin{document}
{\huge Strings to be coloured red} (regardless of how they are formatted, this should e.g. work both for bold and for not bold text) \vspace{3em}
\begin{itemize}
\item \textbf{old, you}
\item \textbf{... and when}
\end{itemize}
\vspace{3em} {\huge Strings to be coloured blue} (regardless of how they are formatted, this should e.g. work both for bold and for not bold text) \vspace{3em}
\begin{itemize}
\item \textbf{you don't}
\end{itemize}
\vspace{3em} {\huge Suggested poem to be automatically coloured (according to text strings and rules predefined above):} \vspace{3em}
\textbf{When you are old, you know that.}\\
When you are old, you know how the story ends without reading.\\
Or maybe you don't know, but you know that the story will end.\\
When you are old, you know what you did wrong.\\
Or maybe you don't know, but you know that you did wrong.\\
You realize that what you have liked is just what you have known.\\
And that you like so much more than you have known.\\
When great surgeons undergo surgeries ...\\
... and when great writers read a book ... \\
... and when great lovers are loved ...\\
Then what?\\
Don't be mistaken: it is not because you are dead\\
that you will not do wrong!\\
People might stumble over your bones and get terribly injured.\\
Getting older never stops.\\
So donate your body to science, and grow old well.
\end{document}
l3regex, for example, but this depends on the precise structure. – Apr 19 '16 at 10:54