LibreOffice formula to extract First two words if it begins with Mr. Mrs. Dr. etc

I am using Ubuntu 14.04 and LibreOffice 6.3. I manage to extract first word with the following formula thanks to AskLibreoffice =REGEX(A1,"^(\w+).* Vs (\w+).*","$1 Vs $2")
Now I want to extract first two words in case the first word is Mr. Mrs. Dr. M/s etc. For example if the data is Mr. Aniruddha Mohod Vs Dr. Amitabh Bachchan the result should be Mr. Aniruddha Vs Dr. Amitabh

1 Like

What about the “first two words” of Prof Dr. Dr. von der Pfordten ?
Trying to get something meaningful from a compound requires to know a very strict assured syntax.

Hi @AniruddhaMohod, with the following function you achieve your result:

=REGEX(A2,"^(\w+\.? \w+).* Vs (\w+\.? \w+) .*","$1 Vs $2")

However, the function is not compatible with your previous function. It cannot do both.

2 Likes

Try this.

=REGEX( A1 ; "(((\w+\.|M/s) )?\w+).* Vs (((\w+\.|M/s) )?\w+).*" ; "$1 Vs $4" )

And, Corresponding to future questions.
Ask_86222_Regex_Vs.ods (36.6 KB)

That expression unnecessarily uses too many capturing parentheses, which slow down processing and makes counting the captured grouping harder. Can be enhanced to this:

=REGEX(A1; "((?:(?:\w+\.|M/s) )?\w+).* Vs ((?:(?:\w+\.|M/s) )?\w+).*" ; "$1 Vs $2" )
2 Likes

I learned.
Thank you erAck.

1 Like