Regular Expressions 101

Community Patterns

Extract countries names in a spaceless string

0

Regular Expression
PCRE2 (PHP >=7.3)

/
(?:[A-Z]\.)+|[A-Z][a-z]+
/
gm

Description

works with strings such as: U.K.TanzaniaU.S.A., UkraineFrance, GermanyU.S.A.

Issues:

  1. Doesn't work with repeating acronyms, e.g.: U.K.U.S.A.; possible fix: (?:[A-Z].){1,3}|[A-Z][a-z]+
  2. Works only with country's acronyms that have the same number of dots as the number of capital letters
Submitted by whatserface - a year ago