Nikki Luzader
Nikki Luzader

Reputation: 111

How can I detect if the first word in a substring is completely in upper case?

I am trying to write a Regex that will return these results in a google sheet. Both the words upper and case are in the same cell including the comma. I am also looking for a way to accomplish this in Google apps script as well, but it isn't a necessity.

"UPPER, Case"        =TRUE
"Upper, CASE"        =FALSE
"uPPER, case"        =FALSE
"UppEr, caSe"        =FALSE
"UPPER, case"        =TRUE
"uPPer, case"        =FALSE

As you can see, it only returns true if the entire first word is in upper case.

It should also ignore the comma and the second word.

So I tried this:

^[A-Z][A-Z]*\W?

^[A-Z] = check first letter to make sure it is a capital letter
[A-Z]* = any amount of the following letters should be capitalized
\W? = ignore anything following and including the first special character (right?)

Please tell me if I am doing this wrong! I am not getting the expected results.

Upvotes: 0

Views: 126

Answers (3)

Green
Green

Reputation: 2565

Try this Regex:

[A-Z]+[,\s|\s,][[A-Z].{1}|[a-z].{1}[a-z]+

Explanation:
[A-Z]+ - indeed catch all the capital letters. BUT you want to ensure 2 things. First, that there is a word, this why you should use +
,\s - I added this part for your pattern - it has a straight foreword meaning ( a space and comma

Upvotes: 0

nicholishen
nicholishen

Reputation: 3012

Your match regex should look something like this.

^[A-Z]+,\s\w*$

Upvotes: 1

Jabs
Jabs

Reputation: 101

You can try something like this ([A-Z]{1}[A-Z]+)

This code verify if the first letter is uppercase and after verify the rest

You can test your regex code in this site http://www.gethifi.com/tools/regex

Upvotes: 0

Related Questions