[英]matching first two letters of the first two words
I'm using regex + php I would match the first two letters of the first two word of a line 我正在使用正则表达式+ php我会匹配前两个单词的前两个字母
For example for the line : 例如,对于该行:
Mr Christophe Mailler Jean Dupon
I need to match : 我需要匹配:
Ch Ma
I tried this regex : 我试过这个正则表达式:
(?:Mr|Mme)\s+\b([a-zA-Z][a-zA-Z])
But it matches only Ch 但它只匹配Ch
Any help please? 有什么帮助吗?
You need to extend the pattern match the second word, too, like this: 你需要扩展模式匹配第二个单词,如下所示:
(?:Mr|Mme)\s+([a-zA-Z]{2})\S*\s+([a-zA-Z]{2})
I'd also recommend taking advantage of Unicode support in the PCRE engine using a pattern like this: 我还建议使用这样的模式在PCRE引擎中利用Unicode支持 :
(?:Mr|Mme)\s+(\pL{2})\S*\s+(\pL{2})
For example: 例如:
$input = "Mr Christophe Mailler Jean Dupon";
preg_match("/(?:Mr|Mme)\s+(\pL{2})\S*\s+(\pL{2})/iu", $input, $output);
echo $output[1] . ' ' . $output[2]; // Ch Ma
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.