Regex - I want to retrieve the last word or number from a sentence

Hi, I am trying to use Regex within a PDF questionnaire and populate the values from that questionnaire to an online form

I want to be able to grab the last word from the sentence, however, the last word can differ depending on the person filling in the questionnaire.

For example, when extracted the pdf file to text file, one of the questions is, ‘21 Name of Institution ABC School’ - I want to get the value ABC School from that sentence. Most questions values are Yes and No so I also want to be able to retrieve that.

I am new to regex so I am trying to get use to it.

Thank you!

Hi Lisa,

I am not the most experienced here and I really haven’t looked at a Regex solution but what if you do the following:

1- Put that sentence in a String variable.
2- Split that string variable using space character. so arr_var = str_var.split(" ")
3- Extract the last 2 elements of the array

Hi @Lisa_O,

This Regex will suite you perfectly:


This will look at 2 sequences of characters separeted by one space at the end of a line

Won’t work if the text is read in paragraph, but if you take the last match you should be still good

Hi Lisa

Try out this Regex Pattern:

Preview the results here on

If you want to learn Regex - check out my New Users Regex MegaPost


This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.