简体   繁体   English

使用反向引用机智php preg_match_all

[英]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. 我在regex和php中是一个新手,但是我面临一个我无法独自处理的问题。

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 : 并且我使用向后引用来设置我希望这些模式最多3次:

     ([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. 根据https://regex101.com/r/pB3nY7/2,它可以用作javascript regex,但不能用作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 : 并且我在PHP脚本中“保护了” \\:

     '#([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: 为了更加笼统,您可以使用unicode属性:

^(\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. 其中\\p[Lu}代表大写字母, \\p{Ll}小写字母, \\p{P}标点符号。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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