Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match uppercase word character with regex in PHP?

Tags:

regex

php

I know there is the [A-Z] notation.. but I am not sure if [a-z] is the same as \w.

I'd like to match \w, but only if it's uppercase.

This should include all the weird characters like Ę, Ą, Ś, Ć, Ź, Ż, Ś, Ł, Ó, Ń.

like image 839
ioleo Avatar asked Dec 03 '22 20:12

ioleo


2 Answers

You can use Unicode character properties. For example,

'/\p{Lu}/u'

Will match any uppercase letter.

like image 167
p.s.w.g Avatar answered Dec 21 '22 23:12

p.s.w.g


\w is equivalent of this character class:

[a-zA-Z0-9_]

If you want upper case unicode characters only then use this character class:

'/[\p{Lu}\p{N}_]/u'

This will match any one of:

  1. Upper case unicode letter
  2. unicode number
  3. Underscore
like image 23
anubhava Avatar answered Dec 22 '22 00:12

anubhava