编辑我解决我的问题。这就是解决方案
$string = "Hello my Name is Paloppa. Im' 20 And? Hello! Words I Io Man";
// Word boundary before the first uppercase letter, followed by any alphanumeric character
preg_match_all( '/(?<!^)\b[A-Z][a-z]{1,}\b(?!["!?.\\\'])/', $string, $matches);
print_r( $matches[0] );
现在我还有一个问题
每当它找到一个单词时,该单词就被插入到数组的某个位置。
如果我有这样一句话:“你的名字和姓氏是什么?我的名字和姓氏‘是保罗·西里奥和意甲联赛25号感恩节兄弟”,这是我的密码
$string = "Whats is your Name and Surname? My Name And Surname' is Paolo Celio and Serie A Iim 25 Thanksbro Bro Ciao";
// Word boundary before the first uppercase letter, followed by any alphanumeric character
preg_match_all( '/(?<!^)\b([A-Z][a-z]+ +){1,}\b(?!["!?.\\\'])/', $string, $matches);
print_r( $matches[0] );
输出如下所示
Array (
[0] => Name
[1] => Name And Surname
[2] => Paolo Celio
[3] => Serie
[4] => Iim
[5] => Thanksbro Bro
)
为什么它不加入意甲,而不是甲级联赛?为什么最后一个词没有出现在输出中?
谢谢
编辑我解决我的问题,这是我的REGEX
preg_match_all('/(?<!^)\b[A-Z]([a-z0-9A-Z]| [A-Z]){1,}\b(?!["!?.\\\'])/', $string, $matches);
发布于 2019-06-12 18:18:41
你可以使用..
<?php
$test="the Quick brown Fox jumps Over the Lazy Dog";
preg_match_all("/[A-Z][a-z]*/",$test,$op);
$output = implode(' ',$op[0]);
echo $output;
?>
发布于 2019-06-12 18:30:33
要提取完整的单词,您需要使用单词边界和字符类来匹配单词的其余部分,并使用lookbehinds来排除先前的内容:
$string = "Hello my Name is Paloppa. I'm 20 And? Hello! Words' Man";
// Word boundary before the first uppercase letter, followed by any alphanumeric character
preg_match_all( '/(?<!^)(?<!\. )\b[A-Z][a-zA-Z]*\b(?!["!?\\\'])/', $string, $matches);
print_r( $matches[0] );
如果您只需要大写的单词,不需要MixedCase单词,请仅用[a-z]
替换[a-zA-Z]
。
演示here
发布于 2019-06-13 02:21:22
当涉及到边缘情况时,这有点复杂,但我们可以简单地根据所需的输出和输入定义两个char类,可能带有一个单词边界,表达式类似于:
(?=[^I'])\b([A-Z][a-z'é]+)\b
我们会根据我们的案例来扩展它。
测试
$re = '/(?=[^I\'])\b([A-Z][a-z\'é]+)\b/m';
$str = 'Hello my name is Paloppa. I\'m 20 And i love Football.
Hello my name is Chloé. I\'m 20 And i love Football.
Hello my name is Renée O\'neal. I\'m 20 And i love Football.';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
RegEx电路
jex.im可视化正则表达式:
https://stackoverflow.com/questions/56559605
复制相似问题