gpt4 book ai didi

php - 如何在 PHP 中通过正则表达式提取圣经书名、章节和经文编号?

转载 作者:行者123 更新时间:2023-12-04 14:35:30 25 4
gpt4 key购买 nike

我看到这个:PHP preg_match bible scripture format

但我的问题有点不同,因为我想提取那些元素,而不仅仅是匹配它们。而我的模式更复杂:

'John 14:16–17, 25–26'
'John 14:16–17'
'John 14:16'
'John 14 16'
'John 14:16'
'John14 : 16'
'John 14 16'
'John14:   16'
'John14:16—17'
'John14 16 17'
'John14 : 16 17'
'John14 : 16 — 17'
'John 14 16 17'
'约翰福音 14 16 17' -> here is an actual example of unicode text

还应该考虑'-'、':'和' '是全角或半角字符,例如'-'、':'和'∀',我的意思是两者都应该有效。

我想要的是提取John(应该支持unicode)、14、16和17(如果存在)这些元素。

我试过:

$str = '10 : 12 — 15  % 52 .633 __+_+)_01(&( %&@#32$%!85#@60$'; 
preg_match_all('/[\d]+?/isU',$str, $t);

效果不是很好。

然后我尝试了:

preg_match_all("([\u4e00-\u9fa5]+)[^\d\n]*(\d+)[^\d\n]*(\d+)[^\d\n]*(\d*)", "John 14:16", $out);
var_dump($out);

同样无效。

好的,我找到了解决方案,它有效,但我不确定它是否 100% 正确:

preg_match_all('#([\x{4e00}-\x{9fa5}]+)[^\d\n]*(\d+)[^\d\n]*(\d+)[^\d\n]*(\d*)#u', $keyword, $match);

最佳答案

^(\p{L}+)?\s*(\d+)?[\p{Pd}\p{Zs}:]*(\d+)?[\p{Pd}\p{Zs}:]*(\d+)?

你需要 \p{L} 来匹配 unicode 字符。

\p{Zs} 表示任何类型的空格,\p{Pd} 表示任何类型的破折号或连字符。

Live demo

preg_match_all("/^(\p{L}+)?\s*(\d+)?[\p{Pd}\p{Zs}:]*(\d+)?[\p{Pd}\p{Zs}:]*(\d+)?/m", "John 14:16", $out);
var_dump($out);

关于php - 如何在 PHP 中通过正则表达式提取圣经书名、章节和经文编号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20875727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com