mgraph
mgraph

Reputation: 15338

PHP Regex get UPPERCASE Name

I have these names:

John Stuart AGNEW
Magdi Cristiano ALLAM
Josefa AÉNDRÉS BARÉA 
Josefa ANDRES BAREA
Laima Liucija ANDRIKIENĖ

and I want to get name that have all UPPERCASE chars

For example, for John Stuart AGNEW I want to get AGNEW

I faced problem with Josefa AÉNDRÉS BARÉA. I want to get AÉNDRÉS BARÉA

I used this regex: (.*) (.[A-Z]{2,100} .[A-Z]{2,100})

Could someone help?

Upvotes: 0

Views: 625

Answers (4)

daniero
daniero

Reputation: 335

Your first (.*) should be made non-greedy, like this: (.*?), so that this doesn't start accepting letters from the uppercase part of the name.

And then as others mentioned, you could extend the range [A-Z] to [A-ZÁÉÍÓÚÜ]

Upvotes: 0

fedmich
fedmich

Reputation: 5381

I tried this and it might be what you need

<?php

$s = 'John Stuart AGNEW
Magdi Cristiano ALLAM
Josefa AÉNDRÉS BARÉA
Josefa ANDRES BAREA
Laima Liucija ANDRIKIENE
Ronald McDONALD
';

$pat = "/\p{Lu}{2,}.*/";
preg_match_all( $pat, $s, $ms);

foreach( $ms[0] as $m){
    echo $m . "<br />\n";
}

?>

Note, some names like McDONALD are mixed cases, are you going to support these too?

Upvotes: 0

npinti
npinti

Reputation: 52185

According to this \p{Lu} should match any upper case letter. So replacing your regular expression to something like so: (.*) (.\p{Lu}{2,100} .\p{Lu}{2,100}) should work.

I did some modification which should make your regular expression slightly more tolerant. (\p{Lu}{2,100}( \p{Lu}{2,100})?). I however have no experience with PHP so I am unable to test it properly.

I have however, tested it on a Java environment and it worked for me.

Upvotes: 4

Evan Mulawski
Evan Mulawski

Reputation: 55334

I would use:

([A-ZÁÉÍÓÚÜ]{2,})

and then concatenate the matches using:

$result = implode(" ", $matches);

Upvotes: 0

Related Questions