regex for catching only upper-case matches

I need to write a regex that will catch hashtags with capital letters and apostrophes only.

For example, "#([A-Z']+)" will catch "#IS" from the following example:

#IS your glass half-full or half-empty?

BUT unfortunately will also catch "#I" in this example

#Is your glass half-full or half-empty?

I tried this: "#([A-Z]+[^a-z]+)" but it catches "#II"

#IIs your glass half-full or half-empty?

I use python and there are other clever ways of solving this problem, but I would prefer a regex solution

Answers


Use \b to indicate a word boundary. Then only entire capitalized words that start with a # and potentially contain an ' will be matched:

"((\s|^)#[A-Z']+\b)"

This may be helpful to you

^[A-Z]+$

This will find all the Upper case letters in your input

([A-Z])\w+

Need Your Help

Android fragment backstack animation fail after orientation change

android animation android-fragments android-orientation fragment-backstack

I have an activity which hosts two fragments with only one shown at a time. Effectively the user, through different environmental conditions, should be able to toggle between the two at any given t...

Different frames, same size Tkinter

python tkinter frame

I have tried weight options and such for my frame but nothing seemed to work. I have the following tables, in separate frames as I wish to delete the second one depending on the OptionMenu choice. ...