Regex - get only last upper letter

Viewed 36

i try to get only upper letter in the last of this sentence

abc N°12558 to company ABC DEF GH 123

i want take only ABC DEF GH

i try this, but number in last make problem

(.[A-Z\s]+)*$
2 Answers

You may use this regex with a lookahead:

\b[A-Z]+(?:\s+[A-Z]+)*(?=[^A-Z]*$)

RegEx Demo

RegEx Details:

  • \b: Word boundary
  • [A-Z]+: Match a word with 1+ uppercase letters
  • (?:\s+[A-Z]+)*: Match 0 or more space separated uppercase words
  • (?=[^A-Z]*$): Lookahead to assert that we don't have any uppercase letters ahead
Related