简体   繁体   中英

Using backreference wit php preg_match_all

I'am quite new in regex and php but I'm facing an issue I can't handle alone.

I've prepared this regex to find patterns starting with upper-case letter. It could sounds something like :

  • capture any pattern that

  • starts with one or more Upper-case letter

  • then one or more any letter or character in the list

  • then a space, or punctuation mark

  • and I use a backreference to set I want those pattern up to 3 times :

     ([A-ZÁÀÂÄÉÈÊËÍÌÎÏÓÒÔÖÚÙÛÜ]{1,}[a-zàáâãäåçèéêëìíîïðòóôõöùúûüýÿ;:«0-9]{1,}[\\s-….?,;]\\1{1,3}) 

According to https://regex101.com/r/pB3nY7/2 it works as a javascript regex but not as a php regex.

I've rade the other posts and make sure :

  • I use single quotes instead of double quotes

  • and I "protected" the \\ in my php script :

     '#([A-ZÁÀÂÄÉÈÊËÍÌÎÏÓÒÔÖÚÙÛÜ]{1,}[a-zàáâãäåçèéêëìíîïðòóôõöùúûüýÿ;:«0-9]{1,}[\\\\s-….?,;]\\\\1{1,3})#' 

But it still can't match any pattern starting with a Upper-case letter.

Thank you in advance for all advice you may provide,

Regards,

Charles

我已经在此网站http://www.phpliveregex.com/上对其进行了测试:

(^[A-ZÁÀÂÄÉÈÊËÍÌÎÏÓÒÔÖÚÙÛÜ]{1,}[a-zàáâãäåçèéêëìíîïðòóôõöùúûüýÿ;:«0-9]{1,}[\s-….?,;]{1,1}){1,3}

To be more generalist, you could use unicode properties:

^(\p[Lu}+[\p{Ll};:«0-9]+[\s\p{P}]){1,3}

Where \\p[Lu} stands for an uppercase letter, \\p{Ll} a lowercase letter and \\p{P} a punctuation.

preg_match('/^(\p[Lu}+[\p{Ll};:«0-9]+[\s\p{P}]){1,3}/', $string, $match);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM