Question: How can I match (with one regex) uppercase phrases some of which have lowercase words in between? Example:

1). "The following months are casual JUNE, JULY, and AUGUST while all others are not."

```
my match should be: "UPPERCASEWORD1, UPPERCASEWORD2, lowercaseword, UPPERCASEWORD3" (i.e.**JUNE, JULY, and AUGUST**) <=== uppercase words with lowercase in between 

```

2) "the office is closed SATURDAY TO MONDAY thank you."

my match should be "**SATURDAY TO MONDAY**" (i.e "UPPERCASEWORD1 UPPERCASEWORD2 UPPERCASEWORD3") <== all uppercase

In simple terms, I am looking for a regex that would match a phrase which starts and ends with uppercase words but that could also have lowercase words in the middle. 

I need one regex which can do both. 
Thanks!

Answer: 
**Regular expressions don’t recognize the English language**. So it doesn't have an idea about prepositions and punctuation. But you can define a rule that is understandable by a regex.

Like this *It should match any number of upper case letters and commas which can be combined with `N` number of lower case characters*. Assuming the number of lower case characters will hold all the preposition.

```
([A-Z]+[\sa-z,]{0,N})+[A-Z]+

```

Another approach would be if you have a list of preposition you can use it in the regex.

```
([A-Z]+((in|out|or|by|with|and)*[\s,]*)+)+[A-Z]+

```

But this list is not reliable as you don’t know which word will act as preposition. In English any word can act as preposition. It depends on where it's used.
```
/(\b[A-Z]+\b[-,\s]+((and|or|is|on|at|to)\s+)*)*\b[A-Z]+\b/g
             ^^^^   ^^^^^^^^^^^^^^^^^^^^
              |              |
 list of delimiters    list of prepositions

```

will match them, for example. Please note that your requirements are very vague, but you could easily replace the list of prepositions with a general matching like `[a-z]+`.