Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

Regex - get only last upper letter

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-Zs]+)*$
question from:https://stackoverflow.com/questions/65598269/regex-get-only-last-upper-letter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You may use this regex with a lookahead:

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

RegEx Demo

RegEx Details:

  • : 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...