Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE2 (PHP >=7.3)

/
(\b(?'first_word'[A-Z](?!([A-Z0-9]*\b|[[:alnum:]]*_)(?# the word isn't in all caps and doesn't contain underscores))(?:[a-z0-9]*)))|(\G(?'trailing_word'[A-Z][a-z0-9]*))
/
gm

Description

Works with many forms of alphanumeric PascalCase

  • PascalCase
  • Pascal
  • PascalCaseCase
  • Pascal684Case (pascal684_case)
  • Pas45calCase (pas45cal_case)
  • PascalCaseCaseCase (with as many words as you like)
  • PPascal (p_pascal)
  • PascalCCase (pascal_c_case)

What isn't matched

  • camelCase
  • words with '_' underscores in them (SCREAMING_SNAKE_CASE, Pascal_Snake_Case)
  • words without lowercase letters (ALLCAPS, ALL54CAPS)

Limitations

  • all 'Capitalized' words are matched and substituted to 'capitalized'
  • PascalCase with non-alphanumeric characters isn't matched
Submitted by inesvar - 8 months ago