0

Is it possible to write a regular expression that would match any words in a string that have TWO capital letters in the beginning of them.

Like:

$string = "DEar John"

2 Answers 2

2

Yes. Use the upper character class, which refers to uppercase characters:

http://php.net/manual/en/regexp.reference.character-classes.php

([[:upper:]][[:upper:]][[:alpha:]]*)

Tested on http://regex101.com/ in PHP PCRE mode.

Sign up to request clarification or add additional context in comments.

Comments

1

Since you seem to label in your question "match any words in a string that have two capital letters in the beginning of them", you can use the Unicode Property \p{Lu} which matches an uppercase letter that has a lowercase variant and the Unicode Property \p{Ll} which matches a lowercase letter that has an uppercase variant.

/\p{Lu}{2}[\pLu\pLl]*/u

However, you could as well go with a basic regular expression.

/[A-Z]{2}(?i:[a-z]*)/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.